394. 字符串解码
作者:互联网
看见这个头疼,直接想到了编译原理的内容,后面看了答案,字符串头疼,下次注意了,这次记忆了
但是c++的字符串处理,我一直没有记住,不去记忆就记不住
stoi(string) 将字符串转化为int
stack的函数 pop() top() push() empty()
string + ,* , length()
拼接
string x="0123456789";
cout<<x.substr()<<endl; //默认全部字符, 输出:0123456789
cout<<x.substr(5)<<endl;//截取从下标5开始到结尾的全部字符, 输出:56789
cout<<x.substr(0,5)<<endl;//从下标为0开始向后截取5位, 输出: 01234
查看代码
class Solution {
public:
string decodeString(string s) {
string result = "";
string temp_num = "";
string temp_word = "";
stack<string>stk;
stack<int>itk;
int nums = 0;
int times = 0;
while(nums<s.length()){
if(isdigit(s[nums])){
temp_num += s[nums];
nums++;
}
else if(isalpha(s[nums])){
temp_word += s[nums];
nums++;
}
else if (s[nums] == '['){
times = stoi( temp_num);
temp_num = "";
itk.push(times);
stk.push(temp_word);
temp_word="" ;
nums++;
}
else{
times = itk.top();
itk.pop();
cout<<times;
for(int i=0;i<times;i++){
stk.top()+=temp_word;
cout<<stk.top();
}
temp_word = stk.top();
stk.pop();
nums++;
}
}
return temp_word;
}
};
标签:cout,temp,nums,int,解码,394,字符串,string 来源: https://www.cnblogs.com/jozon/p/15743171.html