一个去括号的算法题,有人会吗

在表达式值不变的情况下,将输入的表达式中的括号全去掉,并输出去掉括号后的表达式。
比如输入 (a-(b+c)),输出 a-b-c;
再比如输入 ((a+b)-(c-d))-(e+f) ,输出 a+b-c+d-e-f。
#Java工程师##C++工程师#
全部评论
static String removeParentheses(String input){ int length = input.length(); char[] array = input.toCharArray(); int count = 0; Stack<Integer> parenthesesStack = new Stack<>(); StringBuilder sb = new StringBuilder(); for (int i=0; i< length; i++){ switch (array[i]){ case '-': sb.append(((count&1) == 1)?'+':'-'); if(i < length - 1 && array[i+1] == '('){ count ++; parenthesesStack.push(i+1); i++; } break; case ')': int lastLeftIndex = parenthesesStack.pop(); if (lastLeftIndex > 0 && array[lastLeftIndex - 1] == '-') { count--; } break; case '+': sb.append(((count&1) == 1)?'-':'+'); break; case '(': parenthesesStack.push(i); break; default: sb.append(array[i]); } } return sb.toString(); }
点赞 回复 分享
发布于 2018-03-01 15:25
std::string remove_parentheses(const std::string& _str) { auto s = _str; int n = s.size(); std::unordered_map<char, int> pth_cnt; // '('和')'的数量 auto init_pth_cnt = [&pth_cnt] { pth_cnt['('] = 1; pth_cnt[')'] = 0; }; auto op_reverse = [](bool b, char& ch) { // '+'或'-'反号 if (b) ch = (ch == '+') ? '-' : (ch == '-' ? '+' : ch); }; for (int i = 0; i < n; i++) { // 括号无'-'前缀 if (s[i] == '(' || s[i] == ')') s[i] = ' '; // 前缀为'-',则括号内'+'和'-'反号 if (s[i] == '-' && s[i + 1] == '(') { s[i + 1] = ' '; init_pth_cnt(); bool bReverse = true; // 是否反号, 若之前'-('数量为偶数则不反号 for (i = i + 2; i < n && pth_cnt['('] > pth_cnt[')']; i++) { char ch = s[i]; op_reverse(bReverse, s[i]); // 上步处理后s[i]可能发生改变,因此之后对ch而非s[i]进行判断 if (ch == '(' || ch == ')') { s[i] = ' '; pth_cnt[ch]++; } if (ch == '-' && s[i + 1] == '(') { bReverse = !bReverse; pth_cnt['(']++; s[++i] = ' '; } } // 结束"...-(...)"中括号内的处理 } } return s; } C++实现,参考了楼上的做法
点赞 回复 分享
发布于 2018-03-01 14:12
不知道这样行不行,随手写了写 string remove_parentheses(string s){     int len = s.length();     for (int i = 0; i < len; i++){         if ((s[i] == '-') && (s[i + 1] == '(')){             s[i + 1] = ' ';             int count = 1;             for (int j = i + 2; j < len; j++){                 if (s[j] == '('){                     count++;                     s[j] = ' ';                 }                 else if (count < 1)break;                 else if (s[j] == ')'){                     count--;                     s[j] = ' ';                 }                 else if (count == 1&&s[j] == '+')                     s[j] = '-';                 else if (count == 1&&s[j] == '-')                     s[j] = '+';             }         }         else if (s[i] == '(' || s[i] == ')')             s[i] = ' ';     }     string res = "";     for (int i = 0; i < len;i++)     if (s[i] != ' ')         res += s[i];     return res; }
点赞 回复 分享
发布于 2018-03-01 11:01
楼主是想问逆波兰表达式吗
点赞 回复 分享
发布于 2018-03-01 11:21
是只有±运算吗?
点赞 回复 分享
发布于 2018-03-01 00:36
对啊,直接便利去掉就行了,又不用配对处理
点赞 回复 分享
发布于 2018-02-28 23:02
用正则表达式进行模式匹配,可以很轻松地处理吧?
点赞 回复 分享
发布于 2018-02-28 21:59
这个不用算法啊。直接字符串遍历处理就好了
点赞 回复 分享
发布于 2018-02-28 21:37

相关推荐

青春运维少年不会梦到...:实习大王
点赞 评论 收藏
分享
热爱生活的咸鱼在吃瓜:个人建议,项目太简单了,实习干的活都是测试的活,反正又没人知道你实习干啥了,你懂吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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