标签:library leetcode val simple amp height color 注意 arraylist
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.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
Note: Do not use the eval
built-in library function.
这道题写的不好,写的太麻烦了。。。
public class Solution { public int calculate(String s) { if (s == null) { return 0; } int result = 0; List<String> tmp = new ArrayList<>(); int num = 0; for (int i = 0; i < s.length(); i++) { if (Character.isDigit(s.charAt(i))) { num = num * 10 + (s.charAt(i) - ‘0‘); } else { if (s.charAt(i) != ‘ ‘) { tmp.add(String.valueOf(num)); num = 0; tmp.add(s.charAt(i) + ""); } } } tmp.add(String.valueOf(num)); for (int i = 0; i < tmp.size(); i++) { int t = 0; if (tmp.get(i).equals("*") || tmp.get(i).equals("/")) { int a = Integer.valueOf(tmp.get(i - 1)); int b = Integer.valueOf(tmp.get(i + 1)); if (tmp.get(i).equals("*")) { t = a * b; } else if (tmp.get(i).equals("/")) { t = a / b; } tmp.remove(i - 1); tmp.remove(i - 1); tmp.remove(i - 1); tmp.add(i - 1, String.valueOf(t)); i = i - 2; } } result += Integer.valueOf(tmp.get(0)); for (int i = 1; i < tmp.size(); i++) { switch (tmp.get(i)) { case "+": result += Integer.valueOf(tmp.get(i + 1)); i++; break; case "-" : result -= Integer.valueOf(tmp.get(i + 1)); i++; break; } } return result; } }
这效率也是没谁了。。。
还是用好的写法再写一遍吧。。。。哭瞎了。。。
看了top solution发现思路还是基本一致的嘛。。。恩。。。。就是我没用stack。。。而且减号可以转换成负数存进去。
思想就是存储的时候就把乘法除法处理好,然后再集体算一遍。注意处理一下遍历到最后的情况。
public class Solution { public int calculate(String s) { if (s == null) { return 0; } s = s + "+"; Stack<Integer> stack = new Stack<>(); char sign = ‘+‘; int num = 0; for (int i = 0; i < s.length(); i++) { if (Character.isDigit(s.charAt(i))) { num = num * 10 + (s.charAt(i) - ‘0‘); } else { if (s.charAt(i) != ‘ ‘) { if (sign == ‘+‘) { stack.push(num); } if (sign == ‘-‘) { stack.push(-num); } if (sign == ‘*‘) { stack.push(num * stack.pop()); } if (sign == ‘/‘) { stack.push(stack.pop() / num); } sign = s.charAt(i); num = 0; } } } int result = 0; while (!stack.isEmpty()) { result += stack.pop(); } return result; } }
这个版本在最后加了一个加号来计算最后一个值,还有top solution的版本
public class Solution { public int calculate(String s) { if (s == null) { return 0; } Stack<Integer> stack = new Stack<>(); char sign = ‘+‘; int num = 0; for (int i = 0; i < s.length(); i++) { if (Character.isDigit(s.charAt(i))) { num = num * 10 + (s.charAt(i) - ‘0‘); } if ((!Character.isDigit(s.charAt(i)) && s.charAt(i) != ‘ ‘) || i == s.length() - 1) { if (sign == ‘+‘) { stack.push(num); } if (sign == ‘-‘) { stack.push(-num); } if (sign == ‘*‘) { stack.push(num * stack.pop()); } if (sign == ‘/‘) { stack.push(stack.pop() / num); } sign = s.charAt(i); num = 0; } } int result = 0; while (!stack.isEmpty()) { result += stack.pop(); } return result; } }
这个稍微慢一点,大概是判断次数比较多吧。
标签:library leetcode val simple amp height color 注意 arraylist
原文地址:http://www.cnblogs.com/aprilyang/p/6404691.html