c++ stack

压缩算法

http://www.nowcoder.com/questionTerminal/ebf85b76361245f4a3ac273e6876c662

class Solution {
public:

    string compress(string str) {
        stack<char> st;
        for (int i = 0; i < str.size(); i++) {
            //HG[3|B[2|CA]]F
            if (str[i] == ']') {
                string tmp;//CA
                while (!st.empty() && st.top() != '|') {
                    tmp = st.top() + tmp;
                    st.pop();
                }
                st.pop();//pop()'|'
                string s1 = "";//s1代表tmp的叠加次数
                while (!st.empty() && st.top() != '[') {
                    s1 = st.top() + s1;
                    st.pop();
                }
                st.pop();//pop'['
                int t1 = stoi(s1);
                string s2 = "";//s2代表哦解压缩后重新压入栈中的srting
                while (t1) {
                    s2 += tmp;
                    t1--;
                }
                for (int j = 0; j < s2.size(); j++)
                    st.push(s2[j]);
            }else{
                st.push(str[i]);
            }
        }
        string ans = "";
        while (!st.empty()) {
            ans = st.top() + ans;
            st.pop();
        }
        return ans;
    }
};
全部评论

相关推荐

勉勉强强过了两题,没发挥好
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
DKS233:项目写太简单了,你用什么技术实现了什么功能,优化了多少,分了哪些模块,解决了哪些难点,最好分模块写,你写的太模糊了。精通还是少用吧,你确定问你底层你扛的住吗,最好用熟悉。具备良好**意识,这种空话不要写,技能层面,要写就写实在的,比如“熟悉常用数据结构,如,堆,栈,链表,哈希表,平衡树”这种
你的简历改到第几版了
点赞 评论 收藏
分享
评论
7
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务