880:索引处的解码字符串

This commit is contained in:
huangge1199 2021-04-26 15:09:32 +08:00
parent 1cb72906c0
commit 478dc4ce12
2 changed files with 41 additions and 15 deletions

View File

@ -70,24 +70,50 @@ public class DecodedStringAtIndex {
//leetcode submit region begin(Prohibit modification and deletion) //leetcode submit region begin(Prohibit modification and deletion)
class Solution { class Solution {
public String decodeAtIndex(String S, int K) { public String decodeAtIndex(String S, int K) {
StringBuilder str = new StringBuilder(); // StringBuilder str = new StringBuilder();
for (int i = 0; i < S.length(); i++) { // for (int i = 0; i < S.length(); i++) {
// if (Character.isDigit(S.charAt(i))) {
// String temp = str.toString();
// for (int j = 0; j < S.charAt(i) - '0' - 1; j++) {
// str.append(temp);
// if (str.length() >= K) {
// return "" + str.charAt(K - 1);
// }
// }
// } else {
// str.append(S.charAt(i));
// if (str.length() >= K) {
// break;
// }
// }
// }
// return "" + str.charAt(K - 1);
long size = 0;
int length = S.length();
// Find size = length of decoded string
for (int i = 0; i < length; ++i) {
if (Character.isDigit(S.charAt(i))) { if (Character.isDigit(S.charAt(i))) {
String temp = str.toString(); size *= S.charAt(i) - '0';
for (int j = 0; j < S.charAt(i) - '0' - 1; j++) {
str.append(temp);
if (str.length() >= K) {
return "" + str.charAt(K - 1);
}
}
} else { } else {
str.append(S.charAt(i)); size++;
if (str.length() >= K) {
break;
}
} }
} }
return "" + str.charAt(K - 1);
for (int i = length-1; i >= 0; --i) {
K %= size;
if (K == 0 && Character.isLetter(S.charAt(i))) {
return Character.toString(S.charAt(i));
}
if (Character.isDigit(S.charAt(i))) {
size /= S.charAt(i) - '0';
} else {
size--;
}
}
return null;
} }
} }
//leetcode submit region end(Prohibit modification and deletion) //leetcode submit region end(Prohibit modification and deletion)

File diff suppressed because one or more lines are too long