原本我的做法是重建一個string,但看到別人是直接用數的,直接甘拜下風~~~
ref:https://leetcode.com/problems/decoded-string-at-index/solutions/4094647/97-47-reverse-traversal/?envType=daily-question&envId=2023-09-27
class Solution {
public:
string decodeAtIndex(string s, int k) {
long long length = 0;
int i = 0;
while(length < k){
if(isdigit(s[i])){
length *= s[i] - '0';
cout << length;
}else{
length++;
}
i++;
}
for(int j = i - 1; j >= 0; j--){
if(isdigit(s[j])){
length /= s[j] - '0';
k %= length;
}
else{
if(k == 0 || k == length){
return string(1, s[j]);
}
length--;
}
}
return "";
}
};