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 ...
随机推荐
- instruction-set architecture Processor Architecture
Computer Systems A Programmer's Perspective Second Edition We have seen that a processor must execut ...
- App之百度云推送
集成SDK 下载最新的Android SDK压缩包并解压,在新建工程或已有工程中增加百度云推送功能. 我下载的是 ,里面有一个同名的文件夹,文件夹中有 导入云推送jar包和so文件: 将解压后的lib ...
- 费用性支出预分摊form方式和web方式区别
预分摊 1 form方式 费用性支出先通过生产资产行请求 生成一个资产行,如果该资产行分摊到两个资产上则分割成两个资产行,既所谓的针对资产行进行分摊. 2 web方式 费用性支出直接分摊到资产上形成资 ...
- 解决微软的两个恶心问题(VS2008死机、Win2008 WAS无法启动)
1.Visual Studio 2008,在切换到Web设计界面或Html Markup界面时,过一段时间就出现假死,点击任何地方没反应,也关闭不了,只能用任务管理器结束任务. 上网查了下,应该是先装 ...
- Linux环境下配置eclipse,以及创建maven工程
一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...
- 【Android测试】【第二节】ADB——无线模式
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4635782.html 啰嗦两句啊.本来以为adb ...
- Cocos2d-JS替换初始化场景
Cocos2d-js工程默认启动入口为app.js,准备修改为另外一个入口文件如:GameScene.js var GameLayer = cc.Layer.extend({ ctor:functio ...
- Java学习-017-EXCEL 文件读取实例源代码
众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...
- C++经典编程题#4:单词翻转
总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个句子(一行),将句子中的每一个单词翻转后输出. 输入 只有一行,为一个字符串,不超过500个字符.单词之间以空格隔开. 输出 ...
- DMV to track the temp file usage for SQLServer
There are three DMVs you can use to track tempdb usage: sys.dm_db_task_space_usagesys.dm_db_session_ ...