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 ...
随机推荐
- P1083 借教室
思路:前缀和, c表示对于当前的middle, 前缀和 #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 ...
- DML以及DQL的使用方法
DML:数据操作语言 1.插入insert into 单行插入:insert into 表名 (字段名, 字段名,...) values (值, 值, ...) 注:值列表要和字段列表相匹配. ins ...
- Lazarus中TScreen类使用介绍
描述:虚拟屏幕(桌面)可以包含多个物理显示器.Screen对象是鼠标指针.字体.窗体. 对于Delphi兼容的(不可见)DataModules也被列出了. 同时也追踪当前活动窗体窗体.控件和指针. S ...
- RocEDU.阅读.写作
RocEDU.阅读.写作 一.选择图书 <黑客大曝光> 二.读书计划 56天内学习完.时间:2016.01.25--2016.03.20 三.承诺 宋宸宁郑重承诺: 1.在56天内(开始时 ...
- jQuery.mobile.activePage获取当点活动的page
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Erlang ERTS的Trap机制的设计及其用途
出处:http://mryufeng.iteye.com/blog/334744 erlang的trap机制在实现中用的很多,在费时的BIF操作中基本上都可以看到.它的实现需要erl vm的配合.它的 ...
- [LeetCode]题解(python):056-Merge Intervals
题目来源 https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overl ...
- Magento Service Temporarily Unavailable解决方法
插件升级错误或安装失败时 会出现Service Temporarily Unavailable错误,使网站前台后台都无法显示. 在操作完成的情况下,仍然出现这个错误时可以采用以下方法: 1.删除网站站 ...
- sqlserver 中server 函数GETDATE(),DEFAULT用法
alter table Persons add datenow date DEFAULT GETDATE() null, datetimenow datetime DEFAULT GETDATE()n ...
- Java学习-022-Properties 文件数据写入
Properties 配置文件写入主要通过 Properties.setProperty 和 Properties.store 两个方法,此文以一个简单的 properties 文件写入源码做示例. ...