[leetcode]Basic Calculator(java)
来源:互联网 发布:java redis auth 编辑:程序博客网 时间:2024/05/21 17:24
问题描述:
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:
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:
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; }
0 0
- [leetcode]Basic Calculator(java)
- LeetCode-224. Basic Calculator (JAVA)实现计算器
- 【LeetCode】Basic Calculator && Basic Calculator II
- LeetCode - Basic Calculator & Basic Calculator II
- Leetcode之Basic Calculator & Basic Calculator II
- [leetcode] Basic Calculator
- Basic Calculator - LeetCode 224
- LeetCode Basic Calculator
- leetcode:Basic Calculator
- [leetcode][math] Basic Calculator
- [LeetCode] Basic Calculator
- Leetcode 224: Basic Calculator
- leetcode 224 Basic Calculator
- [LeetCode] Basic Calculator II
- [leetcode] Basic Calculator II
- [Leetcode] Basic Calculator
- leetcode Basic Calculator
- leetcode Basic Calculator II
- BAE使用git push出现错误:fatal: The remote end hung up unexpectedly
- Masonry介绍与使用实践:快速上手Autolayout
- Android-Universal-Image-Loader 图片异步加载类库的使用
- android开发笔记之mk文件
- Android View绘制流程
- [leetcode]Basic Calculator(java)
- Swift2学习:Swift概览1-简单的变量
- 红黑树
- MP3文件格式学习
- Activity跳转与传值(传递与回写)
- IOS安装包瘦身
- Android 获取ROOT权限原理解析
- Java 面向对象(封装、继承、多态+面向对象、构造函数、静态代码块)
- 凌驾限期,每天核收1%的迟交金