Movatterモバイル変換


[0]ホーム

URL:


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

Commite76ec1b

Browse files
884_DecodedStringAtIndex884
1 parent6200d32 commite76ec1b

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

‎884_DecodedStringAtIndex884.java‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* An encoded string S is given. To find and write the decoded string to a
3+
* tape, the encoded string is read one character at a time and the following
4+
* 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 (say d), the entire current tape is
8+
* repeatedly written d-1 more times in total.
9+
* Now for some encoded string S, and an index K, find and return the K-th
10+
* letter (1 indexed) in the decoded string.
11+
*
12+
* Example 1:
13+
* Input: S = "leet2code3", K = 10
14+
* Output: "o"
15+
* Explanation:
16+
* The decoded string is "leetleetcodeleetleetcodeleetleetcode".
17+
* The 10th letter in the string is "o".
18+
*
19+
* Example 2:
20+
* Input: S = "ha22", K = 5
21+
* Output: "h"
22+
* Explanation:
23+
* The decoded string is "hahahaha". The 5th letter is "h".
24+
*
25+
* Example 3:
26+
* Input: S = "a2345678999999999999999", K = 1
27+
* Output: "a"
28+
* Explanation:
29+
* The decoded string is "a" repeated 8301530446056247680 times.
30+
* The 1st letter is "a".
31+
*
32+
* Note:
33+
* 2 <= S.length <= 100
34+
* S will only contain lowercase letters and digits 2 through 9.
35+
* S starts with a letter.
36+
* 1 <= K <= 10^9
37+
* The decoded string is guaranteed to have less than 2^63 letters.
38+
*/
39+
40+
publicclassDecodedStringAtIndex884 {
41+
publicStringdecodeAtIndex(StringS,intK) {
42+
char[]chars =S.toCharArray();
43+
intN =chars.length;
44+
long[]lens =newlong[N];
45+
longlen =0;
46+
for (inti=0;i<N;i++) {
47+
charc =chars[i];
48+
if (c >='a' &&c <='z') {
49+
len++;
50+
}else {
51+
intre =Character.getNumericValue(c);
52+
len *=re;
53+
}
54+
lens[i] =len;
55+
}
56+
57+
for (inti=N-1;i>=0;i--) {
58+
charc =chars[i];
59+
if (c >='a' &&c <='z') {
60+
if (lens[i] ==K)returnCharacter.toString(c);
61+
}else {
62+
K %=lens[i-1];
63+
if (K ==0) {
64+
intj =i-1;
65+
while (!(chars[j] >='a' &&chars[j] <='z')) {
66+
j--;
67+
}
68+
returnCharacter.toString(chars[j]);
69+
}
70+
}
71+
}
72+
73+
returnCharacter.toString(chars[0]);
74+
}
75+
76+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp