227. Basic Calculator II
Medium
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers,
+
, -
, *
, /
operators and empty spaces
. The integer division should truncate toward zero.
Example 1:
Input: "3+2*2" Output: 7
Example 2:
Input: " 3/2 " Output: 1
Example 3:
Input: " 3+5 / 2 " Output: 5
Note:
- You may assume that the given expression is always valid.
- Do not use the
eval
built-in library function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | //C++: stack 36ms class Solution { public: int calculate(string s) { stack<int> nums; stack<char> ops; int lhs = 0, rhs = 0, n = s.size(); for(int i=0; i<n; ++i){ if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){ ops.push(s[i]); }else if(s[i]!=' '){ rhs = rhs*10 + s[i] - '0'; if(i==n-1 || !isdigit(s[i+1])){ if(ops.empty() || ops.top()=='+' || ops.top()=='-'){ nums.push(rhs); }else{ while(!ops.empty()&&(ops.top()=='*'||ops.top()=='/')){ char op = ops.top(); ops.pop(); lhs = nums.top(); nums.pop(); rhs = (op=='*')?lhs*rhs:lhs/rhs; nums.push(rhs); } } rhs = 0; } } } lhs = 0; while(!ops.empty()){ rhs = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); lhs = (op=='+')?lhs+rhs:lhs-rhs; } return lhs+nums.top(); } }; ]]></script> <script class="brush: js" type="syntaxhighlighter"><![CDATA[ //C++: 16ms class Solution { public: int calculate(string s) { s += "+"; int n = s.size(), i = 0, val = 0, prev = 0, cur = 0; char op = '+'; while(i<n){ if(isdigit(s[i])){ cur = cur*10+s[i++]-'0'; }else if(s[i]!=' '){ if(op=='+'){ val = val+cur; prev = cur; }else if(op=='-'){ val = val-cur; prev = -cur; }else if(op=='*'){ val = val-prev+prev*cur; prev = prev*cur; }else if(op=='/'){ val = val-prev+prev/cur; prev = prev/cur; } op = s[i++]; cur = 0; }else{ i++; } } return val; } }; ]]></script> <script class="brush: js" type="syntaxhighlighter"><![CDATA[ //C++: 16ms class Solution { public: int calculate(string s) { int res = 0, lhs = 0, rhs = 0, n = s.size(); char op = '+'; for(int i=0; i<n; ++i){ if(isdigit(s[i])){ rhs = rhs*10 + s[i] - '0'; if(i==n-1||!isdigit(s[i+1])){ switch(op){ case '+': lhs += rhs; break; case '-': lhs -= rhs; break; case '*': lhs *= rhs; break; case '/': lhs /= rhs; break; } rhs = 0; } }else if(s[i]!=' '){ if(s[i]=='+'||s[i]=='-'){ res += lhs; lhs = 0; } op = s[i]; } } return res + lhs; } }; class Solution { public: int calculate(string s) { int sz = s.size(); int i = 0; int lhs = 0; int prv = 0; char op = '+'; while(i<sz){ if(s[i]==' '){i++; continue;} int rhs = 0; while(i<sz && isdigit(s[i])){ rhs = rhs*10+(s[i]-'0'); i++; } switch(op){ case '+': lhs += rhs; prv = rhs; break; case '-': lhs -= rhs; prv = (-1)*rhs; break; case '*': lhs -= prv; lhs += prv*rhs; prv = prv*rhs; break; case '/': lhs -= prv; lhs += prv/rhs; prv = prv/rhs; break; default:break; } if(i<sz) op = s[i++]; } return lhs; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | class Solution { public int calculate(String s) { Stack<Integer> nums = new Stack<>(); Stack<Character> ops = new Stack<>(); int v = 0; boolean hasV = false; s += '+'; for(char c:s.toCharArray()){ if(c==' ') continue; else if(Character.isDigit(c)){ v = v*10 + c - '0'; hasV = true; }else{//'+', '-' if(hasV) nums.push(v); v = 0; hasV = false; while(!ops.empty() && precedence(ops.peek())>=precedence(c)) evaluate(nums, ops); ops.push(c); } } return nums.pop(); } int precedence(char op){ switch(op){ case '+' : return 0; case '-' : return 0; case '*' : return 1; case '/' : return 1; default: return -2; } } void evaluate(Stack<Integer> nums, Stack<Character> ops){ int r = nums.pop(); int l = nums.pop(); char op = ops.pop(); int result = 0; switch (op) { case '+' : result = l+r; break; case '-' : result = l-r; break; case '*' : result = l*r; break; case '/' : result = l/r; break; default: break; } nums.push(result); } } |
No comments:
Post a Comment