LeetCode 227. Basic Calculator II
来源:互联网 发布:博雅云计算 培训 编辑:程序博客网 时间:2024/05/22 11:52
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +
, -
, *
, /
operators and empty spaces. The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7" 3/2 " = 1" 3+5 / 2 " = 5
Note: Do not use the eval
built-in library function.
This is one of the most tedious coding problem.
bool isOPTR(char s){ if(s == '+' || s == '-' || s == '*' || s == '/' || s == '\0' || s == '#') return true; else return false; } char getPriority(char a, char b){ int i, j; char priority[][5] = { {'>', '>', '<', '<', '>'}, {'>', '>', '<', '<', '>'}, {'>', '>', '>', '>', '>'}, {'>', '>', '>', '>', '>'}, {'<', '<', '<', '<', '='}}; // it is pretty cool here to make a table.<img alt="得意" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/proud.gif" /> switch(a){ case '+': i = 0; break; case '-': i = 1; break; case '*': i = 2; break; case '/': i = 3; break; case '#': i = 4; break; } switch(b){ case '+': j = 0; break; case '-': j = 1; break; case '*': j = 2; break; case '/': j = 3; break; case '\0': j = 4; break; } return priority[i][j]; } int Operation(int a, char theta, int b){ if(theta == '+') return (a + b); else if(theta == '-') return (a - b); else if(theta == '*') return (a * b); else return (a / b); } int calculate(string s) { int len = s.size(); if(len == 0) return 0; int i = 0; stack<char> OPTR; OPTR.push('#'); stack<int> OPND; while(s[i] != '\0' || OPTR.top()!= '#'){ if(s[i] == ' '){i++; continue;} else if(!isOPTR(s[i])){ int sum = int(s[i] - '0'); int j = i + 1; while(s[j] != '\0' && s[j] != ' ' && !isOPTR(s[j])){ sum = sum * 10 + int(s[j] - '0'); j++; } OPND.push(sum); i = j; continue; }else{ switch(getPriority(OPTR.top(), s[i])){ case '<': OPTR.push(s[i]); i++; break; case '=': OPTR.pop(); // this one is actually not very necessary. i++; break; case '>': char theta = OPTR.top(); OPTR.pop(); int b = OPND.top(); OPND.pop(); int a = OPND.top(); OPND.pop(); OPND.push(Operation(a, theta, b)); break; } // switch } // else } // while return OPND.top(); }
0 0
- [leetcode] 227.Basic Calculator II
- [leetcode] 227. Basic Calculator II
- Leetcode-227.Basic Calculator II
- Leetcode 227. Basic Calculator II
- 227. Basic Calculator II LeetCode
- leetcode 227. Basic Calculator II
- LeetCode *** 227. Basic Calculator II
- LeetCode 227. Basic Calculator II
- leetcode.227. Basic Calculator II
- LeetCode-227.Basic Calculator II
- leetcode 227. Basic Calculator II
- 【Leetcode】227. Basic Calculator II
- [LeetCode]227. Basic Calculator II
- Leetcode 227. Basic Calculator II
- leetcode:227. Basic Calculator II
- LeetCode 227. Basic Calculator II
- [leetcode]227. Basic Calculator II
- [LeetCode]227. Basic Calculator II
- 关于 This tag and its children can be replaced by one <TextView/> and a compound drawable 的问题
- [设计模式]——静态代理
- Ubuntu14.04搭建PXE服务器过程
- java编译异常,运行异常的区别
- ym—— Android 5.0学习之ListView升级版RecyclerView
- LeetCode 227. Basic Calculator II
- Java中hashCode的作用
- 八皇后问题
- 最近一段时间关于深度学习知识的梳理
- 32bit 设备crash的问题
- GC垃圾回收算法
- java中关键字volatile的作用
- php基础留言板的注意细节(使用txt做简单的数据库)。
- libsvm中的scale命令