lintcode-将表达式转换为逆波兰表达式-370
2015-10-11 22:46
363 查看
给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号)。
样例 对于
样例 对于
[3 - 4 + 5]的表达式(该表达式可表示为["3", "-", "4", "+", "5"]),返回
[3 4 - 5 +](该表达式可表示为 ["3", "4", "-", "5", "+"])
class Solution { public: inline void init() { level["+"]=0; level["-"]=0; level["*"]=1; level["/"]=1; level["["]=2; level["]"]=2; level["("]=3; level[")"]=3; } inline bool check_number(const string &s) { for(auto &e:s) if(e>'9'||e<'0') return false; return true; } vector<string> convertToRPN(vector<string> &arr) { init(); vector<string> result; stack <string> s; for(auto &e:arr){ if(check_number(e)) result.push_back(e); else if(e==")"){ while(s.top()!="("){ result.push_back(s.top()); s.pop(); } s.pop(); }else if(e=="]"){ while(s.top()!="["){ result.push_back(s.top()); s.pop(); } s.pop(); }else{ while(!s.empty()&&s.top()!="["&&s.top()!="("&&level[e]<=level[s.top()]){ result.push_back(s.top()); s.pop(); } s.push(e); } } while(!s.empty()) { result.push_back(s.top()); s.pop(); } return result; } private: map<string,int> level; };
相关文章推荐
- Java使用外部库=>长期更新
- lvs-dr,https
- HDU 5119 Happy Matt Friends (01背包)
- 扫描线 求挖空矩形合并面积 hdu3265 Posters
- jvm
- bzoj4278: [ONTAK2015]Tasowanie
- 构建android UI 学习心得,摘要
- 软件开发生命周期的管理 (转)
- 大龄屌丝自学笔记--Java零基础到菜鸟--033
- Myeclipse中SVN的使用
- hdu 5500 Reorder the Books(贪心)
- 扫描线 求多个矩形合并后的周长 hdu1828 Picture
- lvs-nat,https
- java 枚举类型
- request.getPathInfo();
- poj3308 最小割
- hdu 5499 SDOI(水)
- 采购管理中,包括哪些内容?
- VIM的使用
- Windows下配置Java环境变量