From 9ec4c08f8f5a4b8a561746a1b4ff61ba260e5577 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 30 Mar 2025 09:21:03 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3498 No.3498.Reverse Degree of a String --- .../3498.Reverse Degree of a String/README.md | 58 +++++++++++++++++-- .../README_EN.md | 58 +++++++++++++++++-- .../Solution.cpp | 12 ++++ .../Solution.go | 7 +++ .../Solution.java | 11 ++++ .../Solution.py | 7 +++ .../Solution.ts | 8 +++ 7 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 solution/3400-3499/3498.Reverse Degree of a String/Solution.cpp create mode 100644 solution/3400-3499/3498.Reverse Degree of a String/Solution.go create mode 100644 solution/3400-3499/3498.Reverse Degree of a String/Solution.java create mode 100644 solution/3400-3499/3498.Reverse Degree of a String/Solution.py create mode 100644 solution/3400-3499/3498.Reverse Degree of a String/Solution.ts diff --git a/solution/3400-3499/3498.Reverse Degree of a String/README.md b/solution/3400-3499/3498.Reverse Degree of a String/README.md index d20a1d8f69df9..8b95ad9ef2e3f 100644 --- a/solution/3400-3499/3498.Reverse Degree of a String/README.md +++ b/solution/3400-3499/3498.Reverse Degree of a String/README.md @@ -130,32 +130,82 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3498.Re -### 方法一 +### 方法一:模拟 + +我们可以模拟字符串中每个字符的反转度。对于每个字符,我们计算它在反转字母表中的位置,然后乘以它在字符串中的位置,最后将所有结果相加即可。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 #### 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; +} ``` diff --git a/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md b/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md index 090ca3fb1d0d0..0d2ba88add1b5 100644 --- a/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md +++ b/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md @@ -128,32 +128,82 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3498.Re -### 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)$. #### 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; +} ``` diff --git a/solution/3400-3499/3498.Reverse Degree of a String/Solution.cpp b/solution/3400-3499/3498.Reverse Degree of a String/Solution.cpp new file mode 100644 index 0000000000000..a3b65cd478c48 --- /dev/null +++ b/solution/3400-3499/3498.Reverse Degree of a String/Solution.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/solution/3400-3499/3498.Reverse Degree of a String/Solution.go b/solution/3400-3499/3498.Reverse Degree of a String/Solution.go new file mode 100644 index 0000000000000..0c488b8bd0212 --- /dev/null +++ b/solution/3400-3499/3498.Reverse Degree of a String/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/3400-3499/3498.Reverse Degree of a String/Solution.java b/solution/3400-3499/3498.Reverse Degree of a String/Solution.java new file mode 100644 index 0000000000000..20756ccf5fe75 --- /dev/null +++ b/solution/3400-3499/3498.Reverse Degree of a String/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3400-3499/3498.Reverse Degree of a String/Solution.py b/solution/3400-3499/3498.Reverse Degree of a String/Solution.py new file mode 100644 index 0000000000000..ef6c1b1f0c778 --- /dev/null +++ b/solution/3400-3499/3498.Reverse Degree of a String/Solution.py @@ -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 diff --git a/solution/3400-3499/3498.Reverse Degree of a String/Solution.ts b/solution/3400-3499/3498.Reverse Degree of a String/Solution.ts new file mode 100644 index 0000000000000..b07f48346e93f --- /dev/null +++ b/solution/3400-3499/3498.Reverse Degree of a String/Solution.ts @@ -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; +}