Skip to content

Commit a0f3043

Browse files
committed
一刷880
1 parent 3e7c3c1 commit a0f3043

6 files changed

Lines changed: 113 additions & 58 deletions

File tree

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6184,13 +6184,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
61846184
//|{doc_base_url}/0879-profitable-schemes.adoc[题解]
61856185
//|Hard
61866186
//|
6187-
//
6188-
//|{counter:codes}
6189-
//|{leetcode_base_url}/decoded-string-at-index/[880. Decoded String at Index^]
6190-
//|{source_base_url}/_0880_DecodedStringAtIndex.java[Java]
6191-
//|{doc_base_url}/0880-decoded-string-at-index.adoc[题解]
6192-
//|Medium
6193-
//|
6187+
6188+
|{counter:codes}
6189+
|{leetcode_base_url}/decoded-string-at-index/[880. Decoded String at Index^]
6190+
|{source_base_url}/_0880_DecodedStringAtIndex.java[Java]
6191+
|{doc_base_url}/0880-decoded-string-at-index.adoc[题解]
6192+
|Medium
6193+
|
61946194

61956195
|{counter:codes}
61966196
|{leetcode_base_url}/boats-to-save-people/[881. Boats to Save People^]
Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,82 @@
11
[#0880-decoded-string-at-index]
2-
= 880. Decoded String at Index
2+
= 880. 索引处的解码字符串
33

4-
{leetcode}/problems/decoded-string-at-index/[LeetCode - Decoded String at Index^]
4+
https://leetcode.cn/problems/decoded-string-at-index/[LeetCode - 880. 索引处的解码字符串^]
55

6-
An encoded string `S` is given. To find and write the _decoded_ string to a tape, the encoded string is read *one character at a time* and the following steps are taken:
6+
给定一个编码字符串 `s` 。请你找出 __ *解码字符串* 并将其写入磁带。解码时,从编码字符串中 *每次读取一个字符*,并采取以下步骤:
77

8+
* 如果所读的字符是字母,则将该字母写在磁带上。
9+
* 如果所读的字符是数字(例如 `d`),则整个当前磁带总共会被重复写 `d-1` 次。
810
9-
* If the character read is a letter, that letter is written onto the tape.
10-
* If the character read is a digit (say `d`), the entire current tape is repeatedly written `d-1` more times in total.
11+
现在,对于给定的编码字符串 `s` 和索引 `k`,查找并返回解码字符串中的第 `k` 个字母。
1112

13+
*示例 1:*
1214

13-
Now for some encoded string `S`, and an index `K`, find and return the `K`-th letter (1 indexed) in the decoded string.
15+
....
16+
输入:s = "leet2code3", k = 10
17+
输出:"o"
18+
解释:
19+
解码后的字符串为 "leetleetcodeleetleetcodeleetleetcode"。
20+
字符串中的第 10 个字母是 "o"。
21+
....
1422

15-
23+
*示例 2:*
1624

25+
....
26+
输入:s = "ha22", k = 5
27+
输出:"h"
28+
解释:
29+
解码后的字符串为 "hahahaha"。第 5 个字母是 "h"。
30+
....
1731

18-
*Example 1:*
19-
20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* S = "leet2code3", K = 10
23-
*Output:* "o"
24-
*Explanation:*
25-
The decoded string is "leetleetcodeleetleetcodeleetleetcode".
26-
The 10th letter in the string is "o".
27-
----
28-
29-
30-
*Example 2:*
31-
32-
[subs="verbatim,quotes,macros"]
33-
----
34-
*Input:* S = "ha22", K = 5
35-
*Output:* "h"
36-
*Explanation:*
37-
The decoded string is "hahahaha". The 5th letter is "h".
38-
----
39-
40-
41-
*Example 3:*
42-
43-
[subs="verbatim,quotes,macros"]
44-
----
45-
*Input:* S = "a2345678999999999999999", K = 1
46-
*Output:* "a"
47-
*Explanation:*
48-
The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a".
49-
----
50-
51-
52-
53-
*Note:*
54-
55-
56-
* `2 <= S.length <= 100`
57-
* `S` will only contain lowercase letters and digits `2` through `9`.
58-
* `S` starts with a letter.
59-
* `1 <= K <= 10^9`
60-
* The decoded string is guaranteed to have less than `2^63` letters.
32+
*示例 3:*
6133

34+
....
35+
输入:s = "a2345678999999999999999", k = 1
36+
输出:"a"
37+
解释:
38+
解码后的字符串为 "a" 重复 8301530446056247680 次。第 1 个字母是 "a"。
39+
....
6240

41+
*提示:*
6342

43+
* `2 \<= s.length \<= 100`
44+
* `s` 只包含小写字母与数字 `2``9`
45+
* `s` 以字母开头。
46+
* `1 \<= k \<= 10^9^`
47+
* 题目保证 `k` 小于或等于解码字符串的长度。
48+
* 解码后的字符串保证少于 `2^63^` 个字母。
6449
6550
51+
== 思路分析
6652

53+
逆向思维,正向求解遇到超大数字会堆栈溢出。需要逆向处理,从后向前,逐步把 `k` 值减小。
6754

6855
[[src-0880]]
56+
[tabs]
57+
====
58+
一刷::
59+
+
60+
--
6961
[{java_src_attr}]
7062
----
7163
include::{sourcedir}/_0880_DecodedStringAtIndex.java[tag=answer]
7264
----
65+
--
66+
67+
// 二刷::
68+
// +
69+
// --
70+
// [{java_src_attr}]
71+
// ----
72+
// include::{sourcedir}/_0880_DecodedStringAtIndex_2.java[tag=answer]
73+
// ----
74+
// --
75+
====
76+
77+
78+
== 参考资料
7379

80+
. https://leetcode.cn/problems/decoded-string-at-index/solutions/3548/suo-yin-chu-de-jie-ma-zi-fu-chuan-by-leetcode/[880. 索引处的解码字符串 - 官方题解^]
81+
. https://leetcode.cn/problems/decoded-string-at-index/solutions/111640/xian-ji-suan-zi-chuan-zhan-kai-zong-chang-du-zai-y/[880. 索引处的解码字符串 - 先計算字串展開總長度,再由原編碼字串由後向前作等價裁減^]
82+
. https://leetcode.cn/problems/decoded-string-at-index/solutions/2773275/880-suo-yin-chu-de-jie-ma-zi-fu-chuan-by-ft95/[880. 索引处的解码字符串 - 无题^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,7 @@ include::0877-stone-game.adoc[leveloffset=+1]
18521852

18531853
// include::0879-profitable-schemes.adoc[leveloffset=+1]
18541854

1855-
// include::0880-decoded-string-at-index.adoc[leveloffset=+1]
1855+
include::0880-decoded-string-at-index.adoc[leveloffset=+1]
18561856

18571857
include::0881-boats-to-save-people.adoc[leveloffset=+1]
18581858

logbook/202601.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,11 @@ endif::[]
12011201
|{doc_base_url}/0877-stone-game.adoc[题解]
12021202
|✅ 数学题或者动态规划。双方都是聪明人,每次都选剩余石堆中的最多数,那么先选的人肯定获胜。
12031203

1204+
|{counter:codes}
1205+
|{leetcode_base_url}/decoded-string-at-index/[880. Decoded String at Index^]
1206+
|{doc_base_url}/0880-decoded-string-at-index.adoc[题解]
1207+
|⭕️ 栈。逆向思维,正向求解遇到超大数字会堆栈溢出。需要逆向处理,从后向前,逐步把 `k` 值减小。
1208+
12041209
|===
12051210

12061211

logbook/corrections.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,11 @@ endif::[]
12831283
|{doc_base_url}/0874-walking-robot-simulation.adoc[题解]
12841284
|⭕️
12851285

1286+
|{counter:codes}
1287+
|{leetcode_base_url}/decoded-string-at-index/[880. Decoded String at Index^]
1288+
|{doc_base_url}/0880-decoded-string-at-index.adoc[题解]
1289+
|⭕️
1290+
12861291
|===
12871292

12881293
截止目前,本轮练习一共完成 {corrections} 道题。
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0880_DecodedStringAtIndex {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2026-08-22 17:19:27
8+
*/
9+
public String decodeAtIndex(String s, int k) {
10+
long[] len = new long[s.length()];
11+
len[0] = 1L;
12+
for (int i = 1; i < s.length(); i++) {
13+
char c = s.charAt(i);
14+
if ('2' <= c && c <= '9') {
15+
len[i] = len[i - 1] * (c - '0');
16+
} else {
17+
len[i] = len[i - 1] + 1;
18+
}
19+
}
20+
for (int i = s.length() - 1; i >= 0; i--) {
21+
char c = s.charAt(i);
22+
k = (int) (k % len[i]);
23+
if (k == 0 && Character.isLetter(c)) {
24+
return String.valueOf(c);
25+
}
26+
}
27+
return null;
28+
}
29+
// end::answer[]
30+
static void main() {
31+
new _0880_DecodedStringAtIndex()
32+
// .decodeAtIndex("ha22", 5);
33+
// .decodeAtIndex("leet2code3", 10);
34+
.decodeAtIndex("y959q969u3hb22odq595", 222280369);
35+
}
36+
}

0 commit comments

Comments
 (0)