zoukankan      html  css  js  c++  java
  • 1021. 删除最外层的括号

    1021. 删除最外层的括号

    有效括号字符串为空 ("")、"(" + A + ")" 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接。例如,"","()","(())()" 和 "(()(()))" 都是有效的括号字符串。

    如果有效字符串 S 非空,且不存在将其拆分为 S = A+B 的方法,我们称其为原语(primitive),其中 A 和 B 都是非空有效括号字符串。

    给出一个非空有效字符串 S,考虑将其进行原语化分解,使得:S = P_1 + P_2 + ... + P_k,其中 P_i 是有效括号字符串原语。

    对 S 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 S 。

    示例 1:

      输入:"(()())(())"
      输出:"()()()"
    解释:
    输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())",
    删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"。


    示例 2:

      输入:"(()())(())(()(()))"
      输出:"()()()()(())"
    解释:
    输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))",
    删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"。


    示例 3:

      输入:"()()"
      输出:""
    解释:
    输入字符串为 "()()",原语化分解得到 "()" + "()",
    删除每个部分中的最外层括号后得到 "" + "" = ""。

    代码1:

    string removeOuterParentheses(string S) {
        string res, tmp;
        int sum = 0;
    
        for (int i = 0; i < S.size(); i++){
            tmp += S[i];
            if (S[i] == '(')
                sum++;
            else
                sum--;
            if (sum == 0){
                res.append(tmp.begin() + 1, tmp.end() - 1);
                tmp = "";
            }
        }
        return res;
    }

    代码2:

    string removeOuterParentheses(string S) {
        int L=1;int R=0;
        string ans;
        for(int i=1;i<S.size();i++){
            if(S[i]=='(')L++;
            else R++;
            if(R!=L)ans.push_back(S[i]);
            else {
                i++;L=1;R=0;
            }
        }
        return ans;
    }

    代码3:

    void del(){
        deque<char>dq;
        deque<char>::iterator itpre,it;
    
        string str;
        cin>>str;
        int len =str.length();
        int lcount=0,rcount=0,count=0;
        for (int  i = 0; i < len; i++)
        {
            if (lcount==0&&rcount==0)
            {
                itpre = dq.begin()+i-count;
                count++;
            }
            
            if (str[i]=='(')
            {
                lcount++;
                dq.push_back('(');
            }else if(str[i]==')')
            {
                rcount++;
                dq.push_back(')');
            }
            if (lcount==rcount)
            {
                dq.erase(itpre);
                dq.pop_back();
                lcount =rcount =0;          
            }
        }
        for (it = dq.begin(); it!=dq.end(); it++)
        {
            cout<<*it<<"";
        }
    }

     

    因上求缘,果上努力~~~~ 作者:每天卷学习,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/13493662.html

  • 相关阅读:
    grep命令详解
    Git命令详解(一)-个人使用
    android intent和intent action大全
    android 监控EditText的变化
    第86章、系统服务之TELEPHONY_SERVICE(从零开始学Android)
    android中getSystemService详解
    关于android各种双卡手机获取imei,imsi的处置(mtk,展讯,高通等)
    Android 获取运营商信息(完整版)-解决高通,MTK等双卡问题
    Android 移动缩放的ImageView
    Android 读写SD卡的文件
  • 原文地址:https://www.cnblogs.com/BlairGrowing/p/13493662.html
Copyright © 2011-2022 走看看