本文主要是介绍leetcode-394,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://leetcode-cn.com/problems/decode-string/
思路:stack
string decodeString(string s) {stack<char> st;string res = "";for (int i = 0;i < s.size(); i++) {if (s[i] == ']') {string temp = "";while(!st.empty() && st.top()!= '[') {temp += st.top();st.pop();}string num = "";st.pop();while(!st.empty() && st.top() >= '0' && st.top() <= '9') {num = st.top() + num;st.pop();}string new_temp = "";int n = atoi(num.c_str());for (int j = 0; j < n; j++) {new_temp = new_temp + temp;}//cout << n << temp << endl;for (int j = new_temp.size() - 1; j >= 0; j--) {st.push(new_temp[j]);}} else {st.push(s[i]);}}while(!st.empty()) {res = st.top() + res;st.pop();}return res;
}
这篇关于leetcode-394的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!