|
1 | 1 | [#0880-decoded-string-at-index] |
2 | | -= 880. Decoded String at Index |
| 2 | += 880. 索引处的解码字符串 |
3 | 3 |
|
4 | | -{leetcode}/problems/decoded-string-at-index/[LeetCode - Decoded String at Index^] |
| 4 | +https://leetcode.cn/problems/decoded-string-at-index/[LeetCode - 880. 索引处的解码字符串^] |
5 | 5 |
|
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` 。请你找出 __ *解码字符串* 并将其写入磁带。解码时,从编码字符串中 *每次读取一个字符*,并采取以下步骤: |
7 | 7 |
|
| 8 | +* 如果所读的字符是字母,则将该字母写在磁带上。 |
| 9 | +* 如果所读的字符是数字(例如 `d`),则整个当前磁带总共会被重复写 `d-1` 次。 |
8 | 10 |
|
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` 个字母。 |
11 | 12 |
|
| 13 | +*示例 1:* |
12 | 14 |
|
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 | +.... |
14 | 22 |
|
15 | | - |
| 23 | +*示例 2:* |
16 | 24 |
|
| 25 | +.... |
| 26 | +输入:s = "ha22", k = 5 |
| 27 | +输出:"h" |
| 28 | +解释: |
| 29 | +解码后的字符串为 "hahahaha"。第 5 个字母是 "h"。 |
| 30 | +.... |
17 | 31 |
|
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:* |
61 | 33 |
|
| 34 | +.... |
| 35 | +输入:s = "a2345678999999999999999", k = 1 |
| 36 | +输出:"a" |
| 37 | +解释: |
| 38 | +解码后的字符串为 "a" 重复 8301530446056247680 次。第 1 个字母是 "a"。 |
| 39 | +.... |
62 | 40 |
|
| 41 | +*提示:* |
63 | 42 |
|
| 43 | +* `2 \<= s.length \<= 100` |
| 44 | +* `s` 只包含小写字母与数字 `2` 到 `9` 。 |
| 45 | +* `s` 以字母开头。 |
| 46 | +* `1 \<= k \<= 10^9^` |
| 47 | +* 题目保证 `k` 小于或等于解码字符串的长度。 |
| 48 | +* 解码后的字符串保证少于 `2^63^` 个字母。 |
64 | 49 |
|
65 | 50 |
|
| 51 | +== 思路分析 |
66 | 52 |
|
| 53 | +逆向思维,正向求解遇到超大数字会堆栈溢出。需要逆向处理,从后向前,逐步把 `k` 值减小。 |
67 | 54 |
|
68 | 55 | [[src-0880]] |
| 56 | +[tabs] |
| 57 | +==== |
| 58 | +一刷:: |
| 59 | ++ |
| 60 | +-- |
69 | 61 | [{java_src_attr}] |
70 | 62 | ---- |
71 | 63 | include::{sourcedir}/_0880_DecodedStringAtIndex.java[tag=answer] |
72 | 64 | ---- |
| 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 | +== 参考资料 |
73 | 79 |
|
| 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. 索引处的解码字符串 - 无题^] |
0 commit comments