|
| 1 | +/** |
| 2 | + * Implement a basic calculator to evaluate a simple expression string. |
| 3 | + * |
| 4 | + * The expression string may contain open ( and closing parentheses ), the |
| 5 | + * plus + or minus sign -, non-negative integers and empty spaces . |
| 6 | + * |
| 7 | + * You may assume that the given expression is always valid. |
| 8 | + * |
| 9 | + * Some examples: |
| 10 | + * "1 + 1" = 2 |
| 11 | + * " 2-1 + 2 " = 3 |
| 12 | + * "(1+(4+5+2)-3)+(6+8)" = 23 |
| 13 | + * Note: Do not use the eval built-in library function. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +public class BasicCalculator224 { |
| 19 | + public int calculate(String s) { |
| 20 | + int L = s.length(); |
| 21 | + int result = 0; |
| 22 | + int start = 0; |
| 23 | + boolean isNum = false; |
| 24 | + boolean doPlus = true; |
| 25 | + for (int i=0; i<L; i++) { |
| 26 | + char c = s.charAt(i); |
| 27 | + if (isDigit(c) && !isNum) { |
| 28 | + start = i; |
| 29 | + isNum = true; |
| 30 | + continue; |
| 31 | + } |
| 32 | + if (isDigit(c)) continue; |
| 33 | + |
| 34 | + if (isNum) { |
| 35 | + result = doCal(s, result, start, i, doPlus); |
| 36 | + isNum = false; |
| 37 | + } |
| 38 | + if (isPlus(c)) { |
| 39 | + doPlus = true; |
| 40 | + } else if (isMunis(c)) { |
| 41 | + doPlus = false; |
| 42 | + } else if (isOpen(c)) { |
| 43 | + List<Integer> l = calculate(s, i+1, L); |
| 44 | + result = doCal(result, l.get(0), doPlus); |
| 45 | + i = l.get(1); |
| 46 | + } |
| 47 | + } |
| 48 | + if (isNum) { |
| 49 | + return doCal(s, result, start, L, doPlus); |
| 50 | + } |
| 51 | + |
| 52 | + return result; |
| 53 | + } |
| 54 | + |
| 55 | + private List<Integer> calculate(String s, int from, int L) { |
| 56 | + int result = 0; |
| 57 | + int start = from; |
| 58 | + int to = from; |
| 59 | + boolean isNum = false; |
| 60 | + boolean doPlus = true; |
| 61 | + for (int i=from; i<L; i++) { |
| 62 | + char c = s.charAt(i); |
| 63 | + if (isDigit(c) && !isNum) { |
| 64 | + start = i; |
| 65 | + isNum = true; |
| 66 | + continue; |
| 67 | + } |
| 68 | + if (isDigit(c)) continue; |
| 69 | + |
| 70 | + if (isNum) { |
| 71 | + result = doCal(s, result, start, i, doPlus); |
| 72 | + isNum = false; |
| 73 | + } |
| 74 | + if (isPlus(c)) { |
| 75 | + doPlus = true; |
| 76 | + } else if (isMunis(c)) { |
| 77 | + doPlus = false; |
| 78 | + } else if (isOpen(c)) { |
| 79 | + List<Integer> l = calculate(s, i+1, L); |
| 80 | + result = doCal(result, l.get(0), doPlus); |
| 81 | + i = l.get(1); |
| 82 | + } else { |
| 83 | + to = i; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + if (isNum) { |
| 88 | + return Arrays.asList(doCal(s, result, start, to, doPlus), to); |
| 89 | + } |
| 90 | + |
| 91 | + return Arrays.asList(result, to); |
| 92 | + } |
| 93 | + |
| 94 | + private int doCal(String s, int previous, int start, int end, boolean doPlus) { |
| 95 | + int num = Integer.parseInt(s.substring(start, end)); |
| 96 | + return doCal(previous, num, doPlus); |
| 97 | + } |
| 98 | + |
| 99 | + private int doCal(int previous, int num, boolean doPlus) { |
| 100 | + if (doPlus) { |
| 101 | + return previous + num; |
| 102 | + } else { |
| 103 | + return previous - num; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private boolean isPlus(char c) { |
| 108 | + return c == '+'; |
| 109 | + } |
| 110 | + private boolean isMunis(char c) { |
| 111 | + return c == '-'; |
| 112 | + } |
| 113 | + private boolean isOpen(char c) { |
| 114 | + return c == '('; |
| 115 | + } |
| 116 | + private boolean isClose(char c) { |
| 117 | + return c == ')'; |
| 118 | + } |
| 119 | + private boolean isDigit(char c) { |
| 120 | + return c >= '0' && c <= '9'; |
| 121 | + } |
| 122 | + private boolean isSpace(char c) { |
| 123 | + return c == ' '; |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * https://discuss.leetcode.com/topic/15816/iterative-java-solution-with-stack |
| 129 | + */ |
| 130 | + public int calculate2(String s) { |
| 131 | + Stack<Integer> stack = new Stack<Integer>(); |
| 132 | + int result = 0; |
| 133 | + int number = 0; |
| 134 | + int sign = 1; |
| 135 | + for(int i = 0; i < s.length(); i++){ |
| 136 | + char c = s.charAt(i); |
| 137 | + if(Character.isDigit(c)){ |
| 138 | + number = 10 * number + (int)(c - '0'); |
| 139 | + }else if(c == '+'){ |
| 140 | + result += sign * number; |
| 141 | + number = 0; |
| 142 | + sign = 1; |
| 143 | + }else if(c == '-'){ |
| 144 | + result += sign * number; |
| 145 | + number = 0; |
| 146 | + sign = -1; |
| 147 | + }else if(c == '('){ |
| 148 | + //we push the result first, then sign; |
| 149 | + stack.push(result); |
| 150 | + stack.push(sign); |
| 151 | + //reset the sign and result for the value in the parenthesis |
| 152 | + sign = 1; |
| 153 | + result = 0; |
| 154 | + }else if(c == ')'){ |
| 155 | + result += sign * number; |
| 156 | + number = 0; |
| 157 | + result *= stack.pop(); //stack.pop() is the sign before the parenthesis |
| 158 | + result += stack.pop(); //stack.pop() now is the result calculated before the parenthesis |
| 159 | + } |
| 160 | + } |
| 161 | + if(number != 0) result += sign * number; |
| 162 | + return result; |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + public int calculate3(String s) { |
| 167 | + char[] chars = s.toCharArray(); |
| 168 | + int i = 0; |
| 169 | + int N = chars.length; |
| 170 | + int sign = 1; |
| 171 | + Stack<String> stack = new Stack<>(); |
| 172 | + while (i < N) { |
| 173 | + char ch = chars[i]; |
| 174 | + if (ch == ' ') { |
| 175 | + i++; |
| 176 | + } else if (ch == '+') { |
| 177 | + sign = 1; |
| 178 | + i++; |
| 179 | + } else if (ch == '-') { |
| 180 | + sign = -1; |
| 181 | + i++; |
| 182 | + } else if (ch == '(') { |
| 183 | + stack.push((sign == 1 ? "+" : "-") + Character.toString(ch)); |
| 184 | + sign = 1; |
| 185 | + i++; |
| 186 | + } else if (ch == ')') { |
| 187 | + int local = 0; |
| 188 | + while (!stack.isEmpty() && !stack.peek().endsWith("(")) { |
| 189 | + local += Integer.valueOf(stack.pop()); |
| 190 | + } |
| 191 | + if (stack.isEmpty()) { |
| 192 | + stack.push(Integer.toString(local)); |
| 193 | + } else { |
| 194 | + String op = stack.pop(); |
| 195 | + if (op.startsWith("+")) { |
| 196 | + stack.push(Integer.toString(local)); |
| 197 | + } else { |
| 198 | + stack.push(Integer.toString(-local)); |
| 199 | + } |
| 200 | + } |
| 201 | + i++; |
| 202 | + } else { |
| 203 | + int j = getNum(chars, i); |
| 204 | + stack.push((sign == 1 ? "+" : "-") + s.substring(i, j)); |
| 205 | + i = j; |
| 206 | + } |
| 207 | + |
| 208 | + } |
| 209 | + int res = 0; |
| 210 | + while (!stack.isEmpty()) res += Integer.valueOf(stack.pop()); |
| 211 | + return res; |
| 212 | + } |
| 213 | + |
| 214 | + private int getNum(char[] chars, int i) { |
| 215 | + int j = i; |
| 216 | + while (j < chars.length && Character.isDigit(chars[j])) j++; |
| 217 | + return j; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * https://leetcode.com/problems/basic-calculator-ii/discuss/63088/Explanation-for-Java-O(n)-time-and-O(1)-space-solution |
| 222 | + */ |
| 223 | + public int calculate4(String s) { |
| 224 | + int pre = 0, curr = 0, sign = 1, op = 0, num = 0; |
| 225 | + |
| 226 | + for (int i = 0; i < s.length(); i++) { |
| 227 | + if (Character.isDigit(s.charAt(i))) { |
| 228 | + num = num * 10 + (s.charAt(i) - '0'); |
| 229 | + if (i == s.length() - 1 || !Character.isDigit(s.charAt(i + 1))) { |
| 230 | + curr = (op == 0 ? num : (op == 1 ? curr * num : curr / num)); |
| 231 | + } |
| 232 | + |
| 233 | + } else if (s.charAt(i) == '*' || s.charAt(i) == '/') { |
| 234 | + op = (s.charAt(i) == '*' ? 1 : -1); |
| 235 | + num = 0; |
| 236 | + |
| 237 | + } else if (s.charAt(i) == '+' || s.charAt(i) == '-') { |
| 238 | + pre += sign * curr; |
| 239 | + sign = (s.charAt(i) == '+' ? 1 : -1); |
| 240 | + op = 0; |
| 241 | + num = 0; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + return pre + sign * curr; |
| 246 | + } |
| 247 | + |
| 248 | +} |
0 commit comments