diff --git a/solution/0800-0899/0880.Decoded String at Index/README.md b/solution/0800-0899/0880.Decoded String at Index/README.md index 15132df56fd28..a2840eb410389 100644 --- a/solution/0800-0899/0880.Decoded String at Index/README.md +++ b/solution/0800-0899/0880.Decoded String at Index/README.md @@ -59,6 +59,12 @@ +**方法一:逆向思维** + +我们可以先计算出解码字符串的总长度 $m,ドル然后从后向前遍历字符串,每次更新 $k$ 为 $k \bmod m,ドル直到 $k$ 为 0ドル$ 且当前字符为字母,返回当前字符。否则,如果当前字符为数字,则将 $m$ 除以该数字。如果当前字符为字母,则将 $m$ 减 1ドル$。 + +时间复杂度 $O(n),ドル其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -66,7 +72,22 @@ ```python - +class Solution: + def decodeAtIndex(self, s: str, k: int) -> str: + m = 0 + for c in s: + if c.isdigit(): + m *= int(c) + else: + m += 1 + for c in s[::-1]: + k %= m + if k == 0 and c.isalpha(): + return c + if c.isdigit(): + m //= int(c) + else: + m -= 1 ``` ### **Java** @@ -74,7 +95,112 @@ ```java +class Solution { + public String decodeAtIndex(String s, int k) { + long m = 0; + for (int i = 0; i < s.length(); ++i) { + if (Character.isDigit(s.charAt(i))) { + m *= (s.charAt(i) - '0'); + } else { + ++m; + } + } + for (int i = s.length() - 1;; --i) { + k %= m; + if (k == 0 && !Character.isDigit(s.charAt(i))) { + return String.valueOf(s.charAt(i)); + } + if (Character.isDigit(s.charAt(i))) { + m /= (s.charAt(i) - '0'); + } else { + --m; + } + } + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + string decodeAtIndex(string s, int k) { + long long m = 0; + for (char& c : s) { + if (isdigit(c)) { + m *= (c - '0'); + } else { + ++m; + } + } + for (int i = s.size() - 1;; --i) { + k %= m; + if (k == 0 && isalpha(s[i])) { + return string(1, s[i]); + } + if (isdigit(s[i])) { + m /= (s[i] - '0'); + } else { + --m; + } + } + } +}; +``` + +### **Go** + +```go +func decodeAtIndex(s string, k int) string { + m := 0 + for _, c := range s { + if c>= '0' && c <= '9' { + m *= int(c - '0') + } else { + m++ + } + } + for i := len(s) - 1; ; i-- { + k %= m + if k == 0 && s[i]>= 'a' && s[i] <= 'z' { + return string(s[i]) + } + if s[i]>= '0' && s[i] <= '9' { + m /= int(s[i] - '0') + } else { + m-- + } + } +} +``` +### **TypeScript** + +```ts +function decodeAtIndex(s: string, k: number): string { + let m = 0n; + for (const c of s) { + if (c>= '1' && c <= '9') { + m *= BigInt(c); + } else { + ++m; + } + } + for (let i = s.length - 1; ; --i) { + if (k>= m) { + k %= Number(m); + } + if (k === 0 && s[i]>= 'a' && s[i] <= 'z') { + return s[i]; + } + if (s[i]>= '1' && s[i] <= '9') { + m = (m / BigInt(s[i])) | 0n; + } else { + --m; + } + } +} ``` ### **...** diff --git a/solution/0800-0899/0880.Decoded String at Index/README_EN.md b/solution/0800-0899/0880.Decoded String at Index/README_EN.md index bed233b7efb9e..62d461f36f0f9 100644 --- a/solution/0800-0899/0880.Decoded String at Index/README_EN.md +++ b/solution/0800-0899/0880.Decoded String at Index/README_EN.md @@ -55,18 +55,144 @@ The 1st letter is "a". ## Solutions +**Solution 1: Reverse Thinking** + +We can first calculate the total length $m$ of the decoded string, then traverse the string from back to front. Each time, we update $k$ to be $k \bmod m,ドル until $k$ is 0ドル$ and the current character is a letter, then we return the current character. Otherwise, if the current character is a number, we divide $m$ by this number. If the current character is a letter, we subtract 1ドル$ from $m$. + +The time complexity is $O(n),ドル where $n$ is the length of the string. The space complexity is $O(1)$. + ### **Python3** ```python - +class Solution: + def decodeAtIndex(self, s: str, k: int) -> str: + m = 0 + for c in s: + if c.isdigit(): + m *= int(c) + else: + m += 1 + for c in s[::-1]: + k %= m + if k == 0 and c.isalpha(): + return c + if c.isdigit(): + m //= int(c) + else: + m -= 1 ``` ### **Java** ```java +class Solution { + public String decodeAtIndex(String s, int k) { + long m = 0; + for (int i = 0; i < s.length(); ++i) { + if (Character.isDigit(s.charAt(i))) { + m *= (s.charAt(i) - '0'); + } else { + ++m; + } + } + for (int i = s.length() - 1;; --i) { + k %= m; + if (k == 0 && !Character.isDigit(s.charAt(i))) { + return String.valueOf(s.charAt(i)); + } + if (Character.isDigit(s.charAt(i))) { + m /= (s.charAt(i) - '0'); + } else { + --m; + } + } + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + string decodeAtIndex(string s, int k) { + long long m = 0; + for (char& c : s) { + if (isdigit(c)) { + m *= (c - '0'); + } else { + ++m; + } + } + for (int i = s.size() - 1;; --i) { + k %= m; + if (k == 0 && isalpha(s[i])) { + return string(1, s[i]); + } + if (isdigit(s[i])) { + m /= (s[i] - '0'); + } else { + --m; + } + } + } +}; +``` + +### **Go** + +```go +func decodeAtIndex(s string, k int) string { + m := 0 + for _, c := range s { + if c>= '0' && c <= '9' { + m *= int(c - '0') + } else { + m++ + } + } + for i := len(s) - 1; ; i-- { + k %= m + if k == 0 && s[i]>= 'a' && s[i] <= 'z' { + return string(s[i]) + } + if s[i]>= '0' && s[i] <= '9' { + m /= int(s[i] - '0') + } else { + m-- + } + } +} +``` +### **TypeScript** + +```ts +function decodeAtIndex(s: string, k: number): string { + let m = 0n; + for (const c of s) { + if (c>= '1' && c <= '9') { + m *= BigInt(c); + } else { + ++m; + } + } + for (let i = s.length - 1; ; --i) { + if (k>= m) { + k %= Number(m); + } + if (k === 0 && s[i]>= 'a' && s[i] <= 'z') { + return s[i]; + } + if (s[i]>= '1' && s[i] <= '9') { + m = (m / BigInt(s[i])) | 0n; + } else { + --m; + } + } +} ``` ### **...** diff --git a/solution/0800-0899/0880.Decoded String at Index/Solution.cpp b/solution/0800-0899/0880.Decoded String at Index/Solution.cpp new file mode 100644 index 0000000000000..9357342a2bcce --- /dev/null +++ b/solution/0800-0899/0880.Decoded String at Index/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + string decodeAtIndex(string s, int k) { + long long m = 0; + for (char& c : s) { + if (isdigit(c)) { + m *= (c - '0'); + } else { + ++m; + } + } + for (int i = s.size() - 1;; --i) { + k %= m; + if (k == 0 && isalpha(s[i])) { + return string(1, s[i]); + } + if (isdigit(s[i])) { + m /= (s[i] - '0'); + } else { + --m; + } + } + } +}; \ No newline at end of file diff --git a/solution/0800-0899/0880.Decoded String at Index/Solution.go b/solution/0800-0899/0880.Decoded String at Index/Solution.go new file mode 100644 index 0000000000000..a3e9192df6e7d --- /dev/null +++ b/solution/0800-0899/0880.Decoded String at Index/Solution.go @@ -0,0 +1,21 @@ +func decodeAtIndex(s string, k int) string { + m := 0 + for _, c := range s { + if c>= '0' && c <= '9' { + m *= int(c - '0') + } else { + m++ + } + } + for i := len(s) - 1; ; i-- { + k %= m + if k == 0 && s[i]>= 'a' && s[i] <= 'z' { + return string(s[i]) + } + if s[i]>= '0' && s[i] <= '9' { + m /= int(s[i] - '0') + } else { + m-- + } + } +} \ No newline at end of file diff --git a/solution/0800-0899/0880.Decoded String at Index/Solution.java b/solution/0800-0899/0880.Decoded String at Index/Solution.java new file mode 100644 index 0000000000000..c4484664d0b95 --- /dev/null +++ b/solution/0800-0899/0880.Decoded String at Index/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public String decodeAtIndex(String s, int k) { + long m = 0; + for (int i = 0; i < s.length(); ++i) { + if (Character.isDigit(s.charAt(i))) { + m *= (s.charAt(i) - '0'); + } else { + ++m; + } + } + for (int i = s.length() - 1;; --i) { + k %= m; + if (k == 0 && !Character.isDigit(s.charAt(i))) { + return String.valueOf(s.charAt(i)); + } + if (Character.isDigit(s.charAt(i))) { + m /= (s.charAt(i) - '0'); + } else { + --m; + } + } + } +} \ No newline at end of file diff --git a/solution/0800-0899/0880.Decoded String at Index/Solution.py b/solution/0800-0899/0880.Decoded String at Index/Solution.py new file mode 100644 index 0000000000000..fa62c1dbb285f --- /dev/null +++ b/solution/0800-0899/0880.Decoded String at Index/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def decodeAtIndex(self, s: str, k: int) -> str: + m = 0 + for c in s: + if c.isdigit(): + m *= int(c) + else: + m += 1 + for c in s[::-1]: + k %= m + if k == 0 and c.isalpha(): + return c + if c.isdigit(): + m //= int(c) + else: + m -= 1 diff --git a/solution/0800-0899/0880.Decoded String at Index/Solution.ts b/solution/0800-0899/0880.Decoded String at Index/Solution.ts new file mode 100644 index 0000000000000..7004bd8742128 --- /dev/null +++ b/solution/0800-0899/0880.Decoded String at Index/Solution.ts @@ -0,0 +1,23 @@ +function decodeAtIndex(s: string, k: number): string { + let m = 0n; + for (const c of s) { + if (c>= '1' && c <= '9') { + m *= BigInt(c); + } else { + ++m; + } + } + for (let i = s.length - 1; ; --i) { + if (k>= m) { + k %= Number(m); + } + if (k === 0 && s[i]>= 'a' && s[i] <= 'z') { + return s[i]; + } + if (s[i]>= '1' && s[i] <= '9') { + m = (m / BigInt(s[i])) | 0n; + } else { + --m; + } + } +}

AltStyle によって変換されたページ (->オリジナル) /