Advertisement
Guest User

Untitled

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. class Solution {
  2.  
  3.     public int calculate(String s) {
  4.         return calculate(s.toCharArray(), 0, s.length() - 1);
  5.     }
  6.  
  7.     private int calculate(char[] s, int l, int r) {
  8.         char lastOp = '.';
  9.         final NumberHolder current = new NumberHolder();
  10.         final NumberHolder prevPrev = new NumberHolder();
  11.         final NumberHolder prev = new NumberHolder();
  12.  
  13.         for (int i = l; i <= r; i++) {
  14.             final char c = s[i];
  15.             final boolean isDigit = c >= '0' && c <= '9';
  16.  
  17.             if (c == '(') {
  18.                 int pCount = 1;
  19.                 int expEnd;
  20.                 for (expEnd = i + 1; expEnd <= r; expEnd++) {
  21.                     if (s[expEnd] == '(') {
  22.                         pCount++;
  23.                     } else if (s[expEnd] == ')') {
  24.                         pCount--;
  25.                     }
  26.                     if (pCount == 0) {
  27.                         break;
  28.                     }
  29.                 }
  30.                 current.setValue(calculate(s, i + 1, expEnd - 1));
  31.                 i = expEnd;
  32.  
  33.             } else if (isDigit) {
  34.                 current.setValue(current.value * 10 + (c - '0'));
  35.             }
  36.  
  37.             if (current.hasValue && (!isDigit || i == r)) {
  38.                 final int newNumber = lastOp == '-' ? -current.value : current.value;
  39.                 current.clear();
  40.                 if (lastOp == '*' || lastOp == '/') {
  41.                     if (lastOp == '*') {
  42.                         prev.setValue(prev.value * newNumber);
  43.                     } else {
  44.                         prev.setValue(prev.value / newNumber);
  45.                     }
  46.                 } else if ((lastOp == '+' || lastOp == '-') && prev.hasValue && prevPrev.hasValue) {
  47.                     prevPrev.setValue(prevPrev.value + prev.value);
  48.                     prev.setValue(newNumber);
  49.                 } else {
  50.                     if (prev.hasValue) {
  51.                         prevPrev.setValue(prev.value);
  52.                     }
  53.                     prev.setValue(newNumber);
  54.                 }
  55.             }
  56.  
  57.             if (c == '+' || c == '-' || c == '*' || c == '/') {
  58.                 lastOp = c;
  59.             }
  60.         }
  61.  
  62.         return prev.value + prevPrev.value;
  63.     }
  64.  
  65.     private static class NumberHolder {
  66.         int value;
  67.         boolean hasValue = false;
  68.  
  69.         public void setValue(int v) {
  70.             value = v;
  71.             hasValue = true;
  72.         }
  73.  
  74.         public void clear() {
  75.             value = 0;
  76.             hasValue = false;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement