Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problem: No.0880 #2014

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
acbin merged 1 commit into main from dev-0880
Nov 25, 2023
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
128 changes: 127 additions & 1 deletion solution/0800-0899/0880.Decoded String at Index/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,148 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:逆向思维**

我们可以先计算出解码字符串的总长度 $m,ドル然后从后向前遍历字符串,每次更新 $k$ 为 $k \bmod m,ドル直到 $k$ 为 0ドル$ 且当前字符为字母,返回当前字符。否则,如果当前字符为数字,则将 $m$ 除以该数字。如果当前字符为字母,则将 $m$ 减 1ドル$。

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

<!-- tabs:start -->

### **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;
}
}
}
```

### **...**
Expand Down
128 changes: 127 additions & 1 deletion solution/0800-0899/0880.Decoded String at Index/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,144 @@ The 1<sup>st</sup> letter is &quot;a&quot;.

## 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)$.

<!-- tabs:start -->

### **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;
}
}
}
```

### **...**
Expand Down
24 changes: 24 additions & 0 deletions solution/0800-0899/0880.Decoded String at Index/Solution.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
};
21 changes: 21 additions & 0 deletions solution/0800-0899/0880.Decoded String at Index/Solution.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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--
}
}
}
23 changes: 23 additions & 0 deletions solution/0800-0899/0880.Decoded String at Index/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
16 changes: 16 additions & 0 deletions solution/0800-0899/0880.Decoded String at Index/Solution.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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
Loading

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