Content-Length: 479433 | pFad | http://github.com/doocs/leetcode/pull/4309/files

99 feat: add solutions to lc problem: No.3498 by yanglbme · Pull Request #4309 · doocs/leetcode · GitHub
Skip to content

feat: add solutions to lc problem: No.3498 #4309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions solution/3400-3499/3498.Reverse Degree of a String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,82 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3498.Re

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们可以模拟字符串中每个字符的反转度。对于每个字符,我们计算它在反转字母表中的位置,然后乘以它在字符串中的位置,最后将所有结果相加即可。

时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def reverseDegree(self, s: str) -> int:
ans = 0
for i, c in enumerate(s, 1):
x = 26 - (ord(c) - ord("a"))
ans += i * x
return ans
```

#### Java

```java

class Solution {
public int reverseDegree(String s) {
int n = s.length();
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 26 - (s.charAt(i - 1) - 'a');
ans += i * x;
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int reverseDegree(string s) {
int n = s.length();
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 26 - (s[i - 1] - 'a');
ans += i * x;
}
return ans;
}
};
```

#### Go

```go
func reverseDegree(s string) (ans int) {
for i, c := range s {
x := 26 - int(c-'a')
ans += (i + 1) * x
}
return
}
```

#### TypeScript

```ts
function reverseDegree(s: string): number {
let ans = 0;
for (let i = 1; i <= s.length; ++i) {
const x = 26 - (s.charCodeAt(i - 1) - 'a'.charCodeAt(0));
ans += i * x;
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
58 changes: 54 additions & 4 deletions solution/3400-3499/3498.Reverse Degree of a String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,82 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3498.Re

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We can simulate the reverse degree of each character in the string. For each character, calculate its position in the reverse alphabet, multiply it by its position in the string, and then sum up all the results.

Time complexity is $O(n)$, where $n$ is the length of the string. Space complexity is $O(1)$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def reverseDegree(self, s: str) -> int:
ans = 0
for i, c in enumerate(s, 1):
x = 26 - (ord(c) - ord("a"))
ans += i * x
return ans
```

#### Java

```java

class Solution {
public int reverseDegree(String s) {
int n = s.length();
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 26 - (s.charAt(i - 1) - 'a');
ans += i * x;
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int reverseDegree(string s) {
int n = s.length();
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 26 - (s[i - 1] - 'a');
ans += i * x;
}
return ans;
}
};
```

#### Go

```go
func reverseDegree(s string) (ans int) {
for i, c := range s {
x := 26 - int(c-'a')
ans += (i + 1) * x
}
return
}
```

#### TypeScript

```ts
function reverseDegree(s: string): number {
let ans = 0;
for (let i = 1; i <= s.length; ++i) {
const x = 26 - (s.charCodeAt(i - 1) - 'a'.charCodeAt(0));
ans += i * x;
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
12 changes: 12 additions & 0 deletions solution/3400-3499/3498.Reverse Degree of a String/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int reverseDegree(string s) {
int n = s.length();
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 26 - (s[i - 1] - 'a');
ans += i * x;
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func reverseDegree(s string) (ans int) {
for i, c := range s {
x := 26 - int(c-'a')
ans += (i + 1) * x
}
return
}
11 changes: 11 additions & 0 deletions solution/3400-3499/3498.Reverse Degree of a String/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int reverseDegree(String s) {
int n = s.length();
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 26 - (s.charAt(i - 1) - 'a');
ans += i * x;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def reverseDegree(self, s: str) -> int:
ans = 0
for i, c in enumerate(s, 1):
x = 26 - (ord(c) - ord("a"))
ans += i * x
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function reverseDegree(s: string): number {
let ans = 0;
for (let i = 1; i <= s.length; ++i) {
const x = 26 - (s.charCodeAt(i - 1) - 'a'.charCodeAt(0));
ans += i * x;
}
return ans;
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/doocs/leetcode/pull/4309/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy