NC199. 字符串解码
描述
示例1
输入:
"3[a]"
输出:
"aaa"
示例2
输入:
"abc"
输出:
"abc"
示例3
输入:
"3[3[b]]"
输出:
"bbbbbbbbb"
C++ 解法, 执行用时: 2ms, 内存消耗: 404KB, 提交时间: 2021-11-13
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ string decodeString(string s) { // write code here string res = ""; stack<int> nums; stack<string> strs; int num = 0; int len = s.size(); for(int i = 0; i< len; ++i){ if(s[i] >= '0' && s[i] <= '9'){ num = num * 10 + s[i] - '0'; } else if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')){ res = res + s[i]; } else if(s[i] == '['){ nums.push(num); num = 0; strs.push(res); res = ""; } else{ int multi = nums.top(); nums.pop(); for(int j = 0; j < multi; ++j) strs.top() += res; res = strs.top(); strs.pop(); } } return res; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 388KB, 提交时间: 2022-03-20
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ string decodeString(string s) { // write code here std::string newStr; std::string str = s; while (decode(str, newStr)) { str = newStr; } return newStr; } bool decode(std::string s, std::string& newStr) { std::string str; std::string subStr; int pos1 = -1; int pos2 = -1; int numPos = -1; bool find = false; for (int i = s.size() - 1; i >= 0; --i) { if (find) { if (s[i] >= '0' && s[i] <= '9') { numPos = i; continue; } break; } if (s[i] == ']') { pos2 = i; } else if (s[i] == '[') { pos1 = i; find = true; } else if (s[i] >= '0' && s[i] <= '9') { numPos = i; } } if (pos1 != -1 && pos2 != -1) { int cc = pos2 - pos1 -1; //字符个数 std::string sss = s.substr(numPos); int dot = std::stoi(s.substr(numPos)); for (int i = 0; i < dot; ++i) { subStr += s.substr(pos1 + 1, cc); } s.erase(s.begin() + numPos, s.begin() + pos2 + 1); for (int j = 0; j < subStr.size(); ++j) { s.insert(s.begin() + numPos + j, subStr[j]); } newStr = s; return true; } if (newStr.empty()) { newStr = s; } return false; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-07-30
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ string decodeString(string s) { // write code here string str = ""; int mutli = 0; string res = ""; stack<int>st; stack<string>alp; for(char c:s){ if(isdigit(c))mutli = mutli*10 + (c - '0'); else if(isalpha(c))res += c; else if(c == '['){ st.push(mutli); alp.push(res); mutli = 0; res = ""; }else if(c == ']'){ int num = st.top(); st.pop(); string tmp = ""; for(int i = 0;i < num;i++){ tmp += res; } res = alp.top() + tmp; alp.pop(); } } return res; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-07-02
class Solution { public: string decodeString(string s) { string res; for (int i = 0; i < s.size();) { if (!isdigit(s[i])) res += s[i ++ ]; else { int j = i; while (isdigit(s[j])) j ++ ; int t = atoi(s.substr(i, j - i).c_str()); int k = j + 1, sum = 0; while (sum >= 0) { if (s[k] == '[') sum ++ ; if (s[k] == ']') sum -- ; k ++ ; } string str = decodeString(s.substr(j + 1, k - j - 2)); while (t -- ) res += str; i = k; } } return res; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-05-13
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ string ans=""; stack<int> nums; stack<string> st; // 考虑测试用例 22[ab10[b]] string decodeString(string s) { // write code here int num=0; for(int i=0;i<s.size();++i){ if(isdigit(s[i])){ num=num*10+s[i]-'0'; } else if(isalpha(s[i])){ ans+=s[i]; } else if(s[i]=='['){ st.push(ans); nums.push(num); num=0; ans=""; } else if(s[i]==']'){ string tmp=""; int t=nums.top(); nums.pop(); for(int j=0;j<t;++j){ tmp+=ans; } ans=st.top()+tmp; st.pop(); } } return ans; } };