978. Basic Calculator

https://www.lintcode.com/problem/basic-calculator/description

  1. public class Solution {
  2. /**
  3. * @param s: the given expression
  4. * @return: the result of expression
  5. */
  6. public int calculate(String s) {
  7. // Write your code here
  8. Stack<Integer> stack = new Stack<Integer>();
  9. int result =0;
  10. int number =0;
  11. int sign =1;
  12.  
  13. for(int i =0;i<s.length();i++){
  14. char c = s.charAt(i);
  15. if(Character.isDigit(c)){
  16. number = 10*number + (int)(c-'0');
  17. }else if(c=='+'){
  18. result +=sign*number;
  19. number =0;
  20. sign =1;
  21. }else if(c == '-'){
  22. result += sign * number;
  23. number = 0;
  24. sign = -1;
  25. }else if(c == '('){
  26. stack.push(result);
  27. stack.push(sign);
  28. sign = 1;
  29. result = 0;
  30. }else if(c == ')'){
  31. result += sign* number;
  32. number =0;
  33. result*=stack.pop();
  34. result+=stack.pop();
  35. }
  36. }
  37.  
  38. if(number!=0){
  39. result +=sign*number;
  40. }
  41.  
  42. return result;
  43. }
  44. }

980. Basic Calculator II

https://www.lintcode.com/problem/basic-calculator-ii/description

  1. public class Solution {
  2. /**
  3. * @param s: the given expression
  4. * @return: the result of expression
  5. */
  6. public int calculate(String s) {
  7. // Write your code here
  8. if(s==null || s.length()==0){
  9. return 0;
  10. }
  11.  
  12. int len = s.length();
  13. Stack<Integer> stack = new Stack<Integer>();
  14. int num =0;
  15. char sign = '+';
  16. for(int i =0;i<len;i++){
  17.  
  18. if(Character.isDigit(s.charAt(i))){
  19. num = num*10 + s.charAt(i)-'0';
  20. }
  21.  
  22. if((!Character.isDigit(s.charAt(i)) && ' '!=s.charAt(i)) ||i==len-1){
  23. if(sign =='-'){
  24. stack.push(-num);
  25. }
  26. if(sign == '+'){
  27. stack.push(num);
  28. }
  29. if(sign == '*'){
  30. stack.push(stack.pop()*num);
  31. }
  32. if(sign == '/'){
  33. stack.push(stack.pop()/num);
  34. }
  35. sign = s.charAt(i);
  36. num =0;
  37. }
  38. }
  39.  
  40. int re =0;
  41. for(int i:stack){
  42. re +=i;
  43. }
  44. return re;
  45.  
  46. }
  47.  
  48. }

849. Basic Calculator III

https://www.lintcode.com/problem/basic-calculator-iii/description

  1. public class Solution {
  2. /**
  3. * @param s: the expression string
  4. * @return: the answer
  5. */
  6. public int calculate(String s) {
  7. // Write your code here
  8. if(s==null || s.length()==0){
  9. return 0;
  10. }
  11.  
  12. Stack<Integer> nums = new Stack<Integer>();
  13. Stack<Character> opr = new Stack<Character>();
  14.  
  15. int num =0;
  16. for(int i=0;i<s.length();i++){
  17. char c = s.charAt(i);
  18. if(c==' '){
  19. continue;
  20. }
  21. if(Character.isDigit(c)){
  22. num = c-'0';
  23. while(i<s.length()-1 && Character.isDigit(s.charAt(i+1))){
  24. num = num*10+ (s.charAt(i+1)-'0');
  25. i++;
  26. }
  27. nums.push(num);
  28. num =0;
  29. }else if(c=='('){
  30. opr.push(c);
  31. }else if(c==')'){
  32. while(opr.peek()!='('){
  33. nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
  34. }
  35. opr.pop();
  36. }else if(c=='+'||c=='-'||c=='*'||c=='/'){
  37. if(!opr.isEmpty()&&needCalFirst(opr.peek(),c)){
  38. nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
  39. }
  40. opr.push(c);
  41. }
  42. }
  43.  
  44. while(!opr.isEmpty()){
  45. nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
  46. }
  47.  
  48. return nums.pop();
  49. }
  50.  
  51. public int calculate(int num1, int num2, char op){
  52. switch(op){
  53. case '+':return num2+num1;
  54. case '-':return num2-num1;
  55. case '*':return num2*num1;
  56. case '/':return num2/num1;
  57. default:return 0;
  58. }
  59. }
  60.  
  61. public boolean needCalFirst(char firstOp, char secondOp){
  62. if(firstOp=='('|| firstOp==')'){
  63. return false;
  64. }
  65.  
  66. if((firstOp=='+'||firstOp=='-')&&(secondOp=='*'||secondOp=='/')){
  67. return false;
  68. }
  69.  
  70. return true;
  71. }
  72. }

368. Expression Evaluation

https://www.lintcode.com/problem/expression-evaluation/description?_from=ladder&&fromId=4

同849 只需注意输入可能不是一个可计算表达式 eg:{'(',')'}

  1. public class Solution {
  2. /**
  3. * @param expression: a list of strings
  4. * @return: an integer
  5. */
  6. public int evaluateExpression(String[] expression) {
  7. // write your code here
  8. if(expression==null || expression.length==0){
  9. return 0;
  10. }
  11.  
  12. String s = "";
  13. for(int i=0;i<expression.length;i++){
  14. s+=expression[i];
  15. }
  16.  
  17. Stack<Integer> nums = new Stack<Integer>();
  18. Stack<Character> opr = new Stack<Character>();
  19.  
  20. int num =0;
  21. for(int i=0;i<s.length();i++){
  22. char c = s.charAt(i);
  23. if(c==' '){
  24. continue;
  25. }
  26. if(Character.isDigit(c)){
  27. num = c-'0';
  28. while(i<s.length()-1 && Character.isDigit(s.charAt(i+1))){
  29. num = num*10+ (s.charAt(i+1)-'0');
  30. i++;
  31. }
  32. nums.push(num);
  33. num =0;
  34. }else if(c=='('){
  35. opr.push(c);
  36. }else if(c==')'){
  37. while(opr.peek()!='('){
  38. if(nums.size()>=2)
  39. nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
  40. }
  41. opr.pop();
  42. }else if(c=='+'||c=='-'||c=='*'||c=='/'){
  43. if(!opr.isEmpty()&&needCalFirst(opr.peek(),c)){
  44. if(nums.size()>=2)
  45. nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
  46. }
  47. opr.push(c);
  48. }
  49. }
  50.  
  51. while(!opr.isEmpty()){
  52. if(nums.size()>=2)
  53. nums.push(calculate(nums.pop(),nums.pop(),opr.pop()));
  54. }
  55.  
  56. if(nums.size()>0){
  57. return nums.pop();
  58. }
  59.  
  60. return 0;
  61. }
  62.  
  63. public int calculate(int num1, int num2, char op){
  64. switch(op){
  65. case '+':return num2+num1;
  66. case '-':return num2-num1;
  67. case '*':return num2*num1;
  68. case '/':return num2/num1;
  69. default:return 0;
  70. }
  71. }
  72.  
  73. public boolean needCalFirst(char firstOp, char secondOp){
  74. if(firstOp=='('|| firstOp==')'){
  75. return false;
  76. }
  77.  
  78. if((firstOp=='+'||firstOp=='-')&&(secondOp=='*'||secondOp=='/')){
  79. return false;
  80. }
  81.  
  82. return true;
  83. }
  84. }

Basic Calculator - Stack(表达式计算器)的更多相关文章

  1. LeetCode OJ:Basic Calculator(基础计算器)

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  2. [LeetCode] 227. Basic Calculator II 基本计算器 II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  3. [LeetCode] Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  4. [LeetCode] 772. Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  5. [LeetCode] Basic Calculator IV 基本计算器之四

    Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...

  6. 227 Basic Calculator II 基本计算器II

    实现一个基本的计算器来计算一个简单的字符串表达式. 字符串表达式仅包含非负整数,+, - ,*,/四种运算符和空格 . 整数除法仅保留整数部分. 你可以假定所给定的表达式总是有效的. 一些例子: &q ...

  7. [LeetCode] 224. Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  8. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  9. [Swift]LeetCode224. 基本计算器 | Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

随机推荐

  1. 用OLEDB读取EXCEL时,单元格内容长度超过255被截断

    https://support.microsoft.com/zh-cn/help/189897/data-truncated-to-255-characters-with-excel-odbc-dri ...

  2. 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 ...

  3. handsontable-utilities

    搜索值 鼠标右键 讲了四个功能:1.row header是否可以右键(rowheader:true):2.删除右键列表的某些值(通过数组定义):3.自定义右键列表和功能(callback,item两个 ...

  4. fedora 系统安装后常用设置

    #表示root命令  $表示普通用户命令 给普通用户添加sudo权限 #visudo    (编辑/etc/sudoers文件的命令) root all = (all) all username al ...

  5. [label][JavaScript扩展] JavaSCript扩展

    http://www.idangero.us/sliders/swiper/ ,swipper for mobile terminal.

  6. [leetcode] 8. Maximum Depth of Binary Tree

    可能是因为我是按难度顺序刷的原因,这个其实在之前的几道题里面已经写过了.题目如下: Given a binary tree, find its maximum depth. The maximum d ...

  7. Mac和 iOS 下的对称和非对称加密算法的使用

    分享在Mac 和 iOS 上使用到的对称和非对称加密算法. 包括RSA,DSA, AES, DES, 3DES 和 blowfish 等等.因为要实现ssh协议, 所以用到了这些算法, 这些算法在ma ...

  8. 编译Hadoop1.0.2历程和解决问题记录

    1.安装eclipse3.6.2, 废止3.7, 这个有很多问题 2.安装eclipse插件ivy You can install Apache IvyDE plugins from the IvyD ...

  9. Android学习之Adapter(数据适配器)

    1.定义     数据适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件. ...

  10. c语言------第一次作业,分支,顺序结构

    1.1思维导图 1.2本章学习体会及代码量学习体 1.2.1学习体会 初次接触C语言,由于比较懒惰,感觉学习脚步跟不上身边的同学,也比较困扰.但伴随着pta上多次显示的##编译错误##,坚持不懈地问舍 ...