Basic Calculator - Stack(表达式计算器)
978. Basic Calculator
https://www.lintcode.com/problem/basic-calculator/description
- public class Solution {
- /**
- * @param s: the given expression
- * @return: the result of expression
- */
- public int calculate(String s) {
- // Write your code here
- Stack<Integer> stack = new Stack<Integer>();
- int result =0;
- int number =0;
- int sign =1;
- for(int i =0;i<s.length();i++){
- char c = s.charAt(i);
- if(Character.isDigit(c)){
- number = 10*number + (int)(c-'0');
- }else if(c=='+'){
- result +=sign*number;
- number =0;
- sign =1;
- }else if(c == '-'){
- result += sign * number;
- number = 0;
- sign = -1;
- }else if(c == '('){
- stack.push(result);
- stack.push(sign);
- sign = 1;
- result = 0;
- }else if(c == ')'){
- result += sign* number;
- number =0;
- result*=stack.pop();
- result+=stack.pop();
- }
- }
- if(number!=0){
- result +=sign*number;
- }
- return result;
- }
- }
980. Basic Calculator II
https://www.lintcode.com/problem/basic-calculator-ii/description
- public class Solution {
- /**
- * @param s: the given expression
- * @return: the result of expression
- */
- public int calculate(String s) {
- // Write your code here
- if(s==null || s.length()==0){
- return 0;
- }
- int len = s.length();
- Stack<Integer> stack = new Stack<Integer>();
- int num =0;
- char sign = '+';
- for(int i =0;i<len;i++){
- if(Character.isDigit(s.charAt(i))){
- num = num*10 + s.charAt(i)-'0';
- }
- if((!Character.isDigit(s.charAt(i)) && ' '!=s.charAt(i)) ||i==len-1){
- if(sign =='-'){
- stack.push(-num);
- }
- if(sign == '+'){
- stack.push(num);
- }
- if(sign == '*'){
- stack.push(stack.pop()*num);
- }
- if(sign == '/'){
- stack.push(stack.pop()/num);
- }
- sign = s.charAt(i);
- num =0;
- }
- }
- int re =0;
- for(int i:stack){
- re +=i;
- }
- return re;
- }
- }
849. Basic Calculator III
https://www.lintcode.com/problem/basic-calculator-iii/description
- public class Solution {
- /**
- * @param s: the expression string
- * @return: the answer
- */
- public int calculate(String s) {
- // Write your code here
- if(s==null || s.length()==0){
- return 0;
- }
- Stack<Integer> nums = new Stack<Integer>();
- Stack<Character> opr = new Stack<Character>();
- int num =0;
- for(int i=0;i<s.length();i++){
- char c = s.charAt(i);
- if(c==' '){
- continue;
- }
- if(Character.isDigit(c)){
- num = c-'0';
- while(i<s.length()-1 && Character.isDigit(s.charAt(i+1))){
- num = num*10+ (s.charAt(i+1)-'0');
- i++;
- }
- nums.push(num);
- num =0;
- }else if(c=='('){
- opr.push(c);
- }else if(c==')'){
- while(opr.peek()!='('){
- nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
- }
- opr.pop();
- }else if(c=='+'||c=='-'||c=='*'||c=='/'){
- if(!opr.isEmpty()&&needCalFirst(opr.peek(),c)){
- nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
- }
- opr.push(c);
- }
- }
- while(!opr.isEmpty()){
- nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
- }
- return nums.pop();
- }
- public int calculate(int num1, int num2, char op){
- switch(op){
- case '+':return num2+num1;
- case '-':return num2-num1;
- case '*':return num2*num1;
- case '/':return num2/num1;
- default:return 0;
- }
- }
- public boolean needCalFirst(char firstOp, char secondOp){
- if(firstOp=='('|| firstOp==')'){
- return false;
- }
- if((firstOp=='+'||firstOp=='-')&&(secondOp=='*'||secondOp=='/')){
- return false;
- }
- return true;
- }
- }
368. Expression Evaluation
https://www.lintcode.com/problem/expression-evaluation/description?_from=ladder&&fromId=4
同849 只需注意输入可能不是一个可计算表达式 eg:{'(',')'}
- public class Solution {
- /**
- * @param expression: a list of strings
- * @return: an integer
- */
- public int evaluateExpression(String[] expression) {
- // write your code here
- if(expression==null || expression.length==0){
- return 0;
- }
- String s = "";
- for(int i=0;i<expression.length;i++){
- s+=expression[i];
- }
- Stack<Integer> nums = new Stack<Integer>();
- Stack<Character> opr = new Stack<Character>();
- int num =0;
- for(int i=0;i<s.length();i++){
- char c = s.charAt(i);
- if(c==' '){
- continue;
- }
- if(Character.isDigit(c)){
- num = c-'0';
- while(i<s.length()-1 && Character.isDigit(s.charAt(i+1))){
- num = num*10+ (s.charAt(i+1)-'0');
- i++;
- }
- nums.push(num);
- num =0;
- }else if(c=='('){
- opr.push(c);
- }else if(c==')'){
- while(opr.peek()!='('){
- if(nums.size()>=2)
- nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
- }
- opr.pop();
- }else if(c=='+'||c=='-'||c=='*'||c=='/'){
- if(!opr.isEmpty()&&needCalFirst(opr.peek(),c)){
- if(nums.size()>=2)
- nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
- }
- opr.push(c);
- }
- }
- while(!opr.isEmpty()){
- if(nums.size()>=2)
- nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
- }
- if(nums.size()>0){
- return nums.pop();
- }
- return 0;
- }
- public int calculate(int num1, int num2, char op){
- switch(op){
- case '+':return num2+num1;
- case '-':return num2-num1;
- case '*':return num2*num1;
- case '/':return num2/num1;
- default:return 0;
- }
- }
- public boolean needCalFirst(char firstOp, char secondOp){
- if(firstOp=='('|| firstOp==')'){
- return false;
- }
- if((firstOp=='+'||firstOp=='-')&&(secondOp=='*'||secondOp=='/')){
- return false;
- }
- return true;
- }
- }
Basic Calculator - Stack(表达式计算器)的更多相关文章
- LeetCode OJ:Basic Calculator(基础计算器)
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [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 IV 基本计算器之四
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...
- 227 Basic Calculator II 基本计算器II
实现一个基本的计算器来计算一个简单的字符串表达式. 字符串表达式仅包含非负整数,+, - ,*,/四种运算符和空格 . 整数除法仅保留整数部分. 你可以假定所给定的表达式总是有效的. 一些例子: &q ...
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [Swift]LeetCode224. 基本计算器 | Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
随机推荐
- 用OLEDB读取EXCEL时,单元格内容长度超过255被截断
https://support.microsoft.com/zh-cn/help/189897/data-truncated-to-255-characters-with-excel-odbc-dri ...
- edis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is configured to save RDB snapshots,
edis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is configured to save RDB snapshots ...
- handsontable-utilities
搜索值 鼠标右键 讲了四个功能:1.row header是否可以右键(rowheader:true):2.删除右键列表的某些值(通过数组定义):3.自定义右键列表和功能(callback,item两个 ...
- fedora 系统安装后常用设置
#表示root命令 $表示普通用户命令 给普通用户添加sudo权限 #visudo (编辑/etc/sudoers文件的命令) root all = (all) all username al ...
- [label][JavaScript扩展] JavaSCript扩展
http://www.idangero.us/sliders/swiper/ ,swipper for mobile terminal.
- [leetcode] 8. Maximum Depth of Binary Tree
可能是因为我是按难度顺序刷的原因,这个其实在之前的几道题里面已经写过了.题目如下: Given a binary tree, find its maximum depth. The maximum d ...
- Mac和 iOS 下的对称和非对称加密算法的使用
分享在Mac 和 iOS 上使用到的对称和非对称加密算法. 包括RSA,DSA, AES, DES, 3DES 和 blowfish 等等.因为要实现ssh协议, 所以用到了这些算法, 这些算法在ma ...
- 编译Hadoop1.0.2历程和解决问题记录
1.安装eclipse3.6.2, 废止3.7, 这个有很多问题 2.安装eclipse插件ivy You can install Apache IvyDE plugins from the IvyD ...
- Android学习之Adapter(数据适配器)
1.定义 数据适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件. ...
- c语言------第一次作业,分支,顺序结构
1.1思维导图 1.2本章学习体会及代码量学习体 1.2.1学习体会 初次接触C语言,由于比较懒惰,感觉学习脚步跟不上身边的同学,也比较困扰.但伴随着pta上多次显示的##编译错误##,坚持不懈地问舍 ...