Skip to content

Commit 099ff6d

Browse files
committed
Add solution and test-cases for problem 880
1 parent c7f70cf commit 099ff6d

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

leetcode/801-900/0880.Decoded-String-at-Index/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [880.Decoded String at Index][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an encoded string `s`. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:
5+
6+
- If the character read is a letter, that letter is written onto the tape.
7+
- If the character read is a digit `d`, the entire current tape is repeatedly written `d - 1` more times in total.
8+
9+
Given an integer `k`, return the k<sup>th</sup> letter (**1-indexed**) in the decoded string.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: s = "leet2code3", k = 10
15+
Output: "o"
16+
Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode".
17+
The 10th letter in the string is "o".
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Decoded String at Index
23-
```go
2422
```
23+
Input: s = "ha22", k = 5
24+
Output: "h"
25+
Explanation: The decoded string is "hahahaha".
26+
The 5th letter is "h".
27+
```
28+
29+
**Example 3:**
2530

31+
```
32+
Input: s = "a2345678999999999999999", k = 1
33+
Output: "a"
34+
Explanation: The decoded string is "a" repeated 8301530446056247680 times.
35+
The 1st letter is "a".
36+
```
2637

2738
## 结语
2839

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "fmt"
4+
5+
func Solution(s string, k int) string {
6+
size := uint64(0)
7+
n := len(s)
8+
for i := 0; i < n; i++ {
9+
if s[i] >= 'a' && s[i] <= 'z' {
10+
size++
11+
continue
12+
}
13+
size = size * uint64(s[i]-'0')
14+
}
15+
kk := uint64(k)
16+
for i := n - 1; i >= 0; i-- {
17+
c := s[i]
18+
kk %= size
19+
if kk == 0 && c >= 'a' && c <= 'z' {
20+
return fmt.Sprintf("%c", c)
21+
}
22+
if c >= 'a' && c <= 'z' {
23+
size--
24+
continue
25+
}
26+
size = size / uint64(c-'0')
27+
}
28+
return ""
529
}

leetcode/801-900/0880.Decoded-String-at-Index/Solution_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
k int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "leet2code3", 10, "o"},
18+
{"TestCase2", "ha22", 5, "h"},
19+
{"TestCase3", "a2345678999999999999999", 1, "a"},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
2627
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2728
c.expect, got, c.inputs)
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)