Lintcode: Expression Evaluation (Basic Calculator III)
- Given an expression string array, return the final result of this expression
- Have you met this question in a real interview? Yes
- Example
- For the expression 2*6-(23+7)/(1+2),
- input is
- [
- "2", "*", "6", "-", "(",
- "23", "+", "7", ")", "/",
- (", "1", "+", "2", ")"
- ],
- return 2
- Note
- The expression contains only integer, +, -, *, /, (, ).
这道题其实应该算Basic Calculator III. 参考了http://blog.csdn.net/nicaishibiantai/article/details/45740649
思路就是两个stack,一个存数字一个存符号。如果遇到数字直接存到数字stack;如果遇到符号,有几种情况:
1.当前符号比上一个符号优先级高,比如* 高于+,那么直接进栈
2.当前符号低于上一个,那么就要把所有已经在stack里面优先于当前符号的全算完,再推进当前符号
3.当前符号是“(”,直接push
4.当前符号是“)”,就要把所有“(”以前的符号全部算完
- public class Solution {
- /**
- * @param expression: an array of strings;
- * @return: an integer
- */
- public int evaluateExpression(String[] expression) {
- // write your code here
- Stack<Integer> integers = new Stack<Integer>();
- Stack<String> ops = new Stack<String>();
- int i = 0;
- while (i < expression.length) {
- String cur = expression[i];
- if (isOp(cur)) { // current string is an op
- if (cur.equals("(")) ops.push(cur);
- else if (cur.equals(")")) {
- while (ops.size()>0 && !ops.peek().equals("(")) {
- integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
- }
- ops.pop();
- }
- else { // +,-,*,/
- while (ops.size()>0 && precede(cur, ops.peek())) {
- integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
- }
- ops.push(cur);
- }
- }
- else integers.push(Integer.parseInt(cur)); // current String is an integer, push to integer stack
- i++;
- }
- while (!ops.isEmpty()) {
- integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
- }
- return integers.isEmpty()? 0 : integers.pop();
- }
- public boolean isOp(String input) {
- if (input.equals("+") || input.equals("-") || input.equals("*")
- || input.equals("/") || input.equals("(") || input.equals(")"))
- return true;
- return false;
- }
- public int calc(int a, int b, String op) {
- if (op.equals("+")) return a+b;
- else if (op.equals("-")) return b-a;
- else if (op.equals("*")) return a*b;
- else return b/a;
- }
- public boolean precede(String a, String b) {
- if (b.equals("*") || b.equals("/")) return true;
- if (b.equals("+") || b.equals("-")) {
- if (a.equals("*") || a.equals("/")) return false;
- else return true;
- }
- return false; //case like (a+b) 到第一个+号时,+和(比应该return false
- }
- };
Lintcode: Expression Evaluation (Basic Calculator III)的更多相关文章
- [LeetCode] Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 772. Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- 【leetcode】Basic Calculator III
题目如下: Implement a basic calculator to evaluate a simple expression string. The expression string may ...
- leetcode 772.Basic Calculator III
这道题就可以结合Basic Calculator中的两种做法了,分别是括号运算和四则运算的,则使用stack作为保持的结果,而使用递归来处理括号内的值的. class Solution { publi ...
- LintCode "Expression Evaluation"
This is sth. for me to learn.. https://github.com/kamyu104/LintCode/blob/master/C++/expression-evalu ...
- Basic Calculator I && II && III
Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...
- Basic Calculator - Stack(表达式计算器)
978. Basic Calculator https://www.lintcode.com/problem/basic-calculator/description public class Sol ...
- [LeetCode] Basic Calculator IV 基本计算器之四
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
随机推荐
- javaWeb中struts开发——Bean标签
1.struts标签库中常用标签 使用myeclise标签可以自动注入,其中,前三个是经常使用的,主要的是logic标签 2.Bean标签 Bean标签主要用来定义和访问JavaBean,在Strut ...
- Java IO包装流如何关闭?
问题: (1)JAVA的IO流使用了装饰模式,关闭最外面的流的时候会自动调用被包装的流的close()方吗? (2)如果按顺序关闭流,是从内层流到外层流关闭还是从外层到内存关闭? 问题(1)解释: ...
- ServletDemo
1. Servlet 接口 继承 Servlet 接口,实现Servlet 接口的 所有抽象方法! 实现类代码 package xw.servlet; import java.io.IOExcepti ...
- PIC12F629帮我用C语言写个程序,控制三个LED亮灭
http://power.baidu.com/question/240873584599025684.html?entry=browse_difficult PIC12F629帮我用C语言写个程序,控 ...
- transform animation transition css3动画
transform 定义 transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 值 应用 如果transform与transition联合起 ...
- Debug BLE application with nRF Sniffer+wireshark
1. Introduction The nRF Bluetooth® Smart Sniffer is a tool for debugging Bluetooth low energy (BLE) ...
- Div自适应高度的方法
http://www.yutheme.cn/website/index.php/content/view/39/63.html div高度自适应是个比较麻烦的问题,在朋友artery那里看到这个文章, ...
- php--字符串函数分类总结
PHP语言中的字符串函数也是一个比较易懂的知识.今天我们就为大家总结了将近12种PHP字符串函数,希望对又需要的朋友有所帮助,增加读者朋友的PHP知识库. 1.查找字符位置函数 strpos($s ...
- readonly=“readonly”与readonly=“true”
<input id="u" readonly /> <input id="u" readonly="readonly" / ...
- 设计模式:享元模式(Flyweight)
定 义:运用共享技术有效地支持大量细粒度的对象. 结构图: 内部状态:在享元对象内部并且不会随环境而改变的共享部分. 外部状态:随环境改变而改变的.不可共享的状态. Flyweight类,具体享元 ...