[leetcode]Basic Calculator(java)
2015-07-07 18:13
656 查看
问题描述:
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
“1 + 1” = 2
” 2-1 + 2 ” = 3
“(1+(4+5+2)-3)+(6+8)” = 23
Note: Do not use the eval built-in library function.
通用算法是使用栈结构+后缀表达式(如代码1),但是看到csdn的一篇博客突然觉得这样好傻。因为这里面只有+和-,那么完全可以将()删除(代码2)。
这里面的思路是:使用一个栈,存放每个当前元素的系数(+1、-1),因为转变符号只有是-才会发生。
代码1:
代码2:
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
“1 + 1” = 2
” 2-1 + 2 ” = 3
“(1+(4+5+2)-3)+(6+8)” = 23
Note: Do not use the eval built-in library function.
通用算法是使用栈结构+后缀表达式(如代码1),但是看到csdn的一篇博客突然觉得这样好傻。因为这里面只有+和-,那么完全可以将()删除(代码2)。
这里面的思路是:使用一个栈,存放每个当前元素的系数(+1、-1),因为转变符号只有是-才会发生。
代码1:
[code] public int calculate(String s) { Stack<Character> charStack = new Stack<Character>(); Stack<Integer> intStack = new Stack<Integer>(); int index = -1; int length = s.length(); char tmpChar; int top1,top2; while((++index)<length){ if((tmpChar=s.charAt(index)) == ' ')//ignore blank continue; switch (tmpChar) { case '(': charStack.push(tmpChar); break; case '-': case '+': if(charStack.isEmpty()) charStack.push(tmpChar); else if(charStack.peek()=='(') charStack.push(tmpChar); else { char op = charStack.pop(); charStack.push(tmpChar); switch (op) { case '+': top1 = intStack.pop(); top2 = intStack.pop(); intStack.push(top1+top2); break; default: top1 = intStack.pop(); top2 = intStack.pop(); intStack.push(top2-top1); break; } } break; case ')': while((tmpChar=charStack.pop())!='('){ switch (tmpChar) { case '+': top1 = intStack.pop(); top2 = intStack.pop(); intStack.push(top1+top2); break; case '-': top1 = intStack.pop(); top2 = intStack.pop(); intStack.push(top2-top1); break; } } break; default://number //form intnumber,and push to intstack int number = 0; while(tmpChar>='0'&&tmpChar<='9'){ number = number*10+tmpChar-'0'; if(++index<length) tmpChar = s.charAt(index); else break; } intStack.push(number); index--; break; } } //process the last one while(!charStack.isEmpty()){ tmpChar = charStack.pop(); switch (tmpChar) { case '+': top1 = intStack.pop(); top2 = intStack.pop(); intStack.push(top2+top1); break;//+ default: top1 = intStack.pop(); top2 = intStack.pop(); intStack.push(top2-top1); break;//- } } return (intStack.isEmpty())?0:intStack.pop(); }
代码2:
[code] public int calculate(String s){ int length = s.length(); Stack<Integer> signStack = new Stack<Integer>(); signStack.push(1);//object signStack.push(1);//firstElement sign int index = -1; char tmpChar; int result = 0; while(++index<length){ if((tmpChar=s.charAt(index))==' ') continue;//ignore blank switch (tmpChar) { case '+': case '(': signStack.push(signStack.peek()); break; case '-': signStack.push(-1*(signStack.peek())); break; case ')': signStack.pop(); break; default://digital int num=0; while(tmpChar>='0'&&tmpChar<='9'){ num=num*10+tmpChar-'0'; if(++index<length) tmpChar = s.charAt(index); else break; } result+=signStack.pop()*num; index--; break; } } return result; }
相关文章推荐
- Spring
- 快速排序算法之JAVA实现
- Java 数组
- java web工程中logback换配置文件的路径
- Predicate和Consumer接口– Java 8中java.util.function包下的接口
- JAVA字符串的GZIP压缩解压缩方法
- java编程思想-——static关键字
- JSTL Java标准模板库
- 使用java连接AD域,验证账号密码是否正确
- 通过前序遍历和中序遍历重建二叉树以及输出后序遍历(Java实现)(二)
- java中的按值传递
- JAVA多态学习3
- Java - 别名机制
- java中InputStream转化为byte[]数组
- JDK源码分析之concurrent包(四) -- CyclicBarrier与CountDownLatch
- struts2学习笔记(5)---自定义拦截器
- java计算时间差及比较时间
- Maven下载Jar包同时下载source和javadoc
- JAVA & Eclipse 笔记
- 基于注解的SpringMVC简单介绍