lintcode--Expression Expand
来源:互联网 发布:m2m 数据采集 编辑:程序博客网 时间:2024/06/18 10:36
题目:Given an expression s
includes numbers, letters and brackets. Number represents the number of repetitions inside the brackets(can be a string or another expression).Please expand expression to be a string.
样例
s = abc3[a]
return abcaaa
s = 3[abc]
return abcabcabc
s = 4[ac]dy
, return acacacacdy
s = 3[2[ad]3[pf]]xyz
, return adadpfpfpfadadpfpfpfadadpfpfpfxyz
class Solution {public: /** * @reverse the word, include words and numbers */ void reverseWord(string &s) { for (int i = 0, j = s.size() - 1; i < j; i++,j--) { swap(s[i], s[j]); } } /** * @param s an expression includes numbers, letters and brackets * @return a string */ string expressionExpand(string& s) { // Write your code here stack<char> res; string val = ""; int len = s.length(); if (len == 0) { return val; } for (int i = 0; i < len; i++) { if (s[i] == ']') { string tmp = ""; while (!res.empty()) { char ch = res.top(); res.pop(); if (ch == '[') { break; } tmp += ch; } string num = ""; while (!res.empty()) { char tmpnum = res.top(); res.pop(); if(isdigit(tmpnum)){ num += tmpnum; } else { res.push(tmpnum); break; } } reverseWord(num); int count = atoi(num.c_str()); string mid = ""; reverseWord(tmp); for (int j = 0; j <count; j++) { mid += tmp; } for (int j = 0; j < mid.length(); j++) { res.push(mid[j]); } } else { res.push(s[i]); } } while (!res.empty()) { val += res.top(); res.pop(); } reverseWord(val); return val; }};
阅读全文
0 0
- lintcode(575)Expression Expand
- lintcode--Expression Expand
- 【LintCode】Expression Expand
- Expression Expand---lintcode
- LintCode:M-Expression Expand
- LintCode-分治-Expression Expand
- lintcode :expression expand using C++
- Expression Expand
- Expression Expand
- Expression Expand
- Expression Expand
- Lintcode - Expression Evaluation
- Lintcode - Expression Evaluation
- [LintCode] Expression Evaluation
- Lintcode - Convert Expression to Reverse Polish Notation
- Lintcode - Convert Expression to Polish Notation
- [LintCode] Convert Expression to Polish Notation
- [LintCode]Convert Expression to Reverse Polish Notation
- 回溯法求和 算法设计
- Elasticsearch基础教程
- 自定义折线图
- 深入焦点处理流程
- mybatis之jdbc的问题
- lintcode--Expression Expand
- 无题
- mybatis中@Param的用法和作用
- 图像的平移和缩放
- normalize、splitText
- 关于RecyclerView中含有CheckBox,Button等控件失去焦点的解决办法
- pip install --upgrade pip 9.0.1 error
- Bot Framework使用Direct Line实现第三方渠道调用Bot服务
- 小白初识线段树(线段树原理+专题练习)