|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -importjava.util.ArrayList; |
4 |
| -importjava.util.List; |
5 |
| -importjava.util.Stack; |
| 3 | +importjava.util.Deque; |
| 4 | +importjava.util.LinkedList; |
6 | 5 |
|
7 | 6 | publicclass_224 {
|
8 | 7 |
|
9 | 8 | publicstaticclassSolution1 {
|
10 |
| - |
| 9 | +/** |
| 10 | + * My complete original solution on 12/23/2021 |
| 11 | + */ |
11 | 12 | publicintcalculate(Strings) {
|
12 |
| -if (s ==null ||s.isEmpty()) { |
13 |
| -return0; |
14 |
| - } |
15 |
| - |
16 |
| -s =s.replaceAll("\\s",""); |
17 |
| -char[]chars =s.toCharArray(); |
18 |
| -List<String>filteredStr =newArrayList(); |
19 |
| -for (inti =0;i <chars.length; ) { |
20 |
| -StringBuildersb =newStringBuilder(); |
21 |
| -while (i <chars.length &&Character.isDigit(chars[i])) { |
22 |
| -sb.append(chars[i]); |
23 |
| -i++; |
24 |
| - } |
25 |
| -if (i ==chars.length) { |
26 |
| -if (sb.toString().length() !=0) { |
27 |
| -filteredStr.add(sb.toString()); |
28 |
| - } |
| 13 | +Deque<String>stack =newLinkedList<>(); |
| 14 | +for (inti =0;i <s.length();i++) { |
| 15 | +if (s.charAt(i) ==' ') { |
| 16 | +continue; |
29 | 17 | }else {
|
30 |
| -if (sb.toString().length() !=0) { |
31 |
| -filteredStr.add(sb.toString()); |
32 |
| - } |
33 |
| -if (chars[i] =='+' ||chars[i] =='-' ||chars[i] =='(' ||chars[i] ==')') { |
34 |
| -filteredStr.add(String.valueOf(chars[i])); |
35 |
| - } |
36 |
| -i++; |
37 |
| - } |
38 |
| - } |
39 |
| - |
40 |
| -for (Stringstr :filteredStr) { |
41 |
| -System.out.print(str); |
42 |
| - } |
43 |
| - |
44 |
| -Stack<String>stack1 =newStack(); |
45 |
| -Stack<String>stack2 =newStack(); |
46 |
| -for (inti =0;i <filteredStr.size(); ) { |
47 |
| -while (i <filteredStr.size() && !filteredStr.get(i).equals(")")) { |
48 |
| -stack1.push(filteredStr.get(i)); |
49 |
| -i++; |
50 |
| - } |
51 |
| -if (i !=filteredStr.size()) { |
52 |
| -while (!stack1.isEmpty() && !stack1.peek().equals("(")) { |
53 |
| -stack2.push(stack1.pop()); |
54 |
| - } |
55 |
| -stack1.pop(); |
56 |
| -intexp =0; |
57 |
| -while (!stack2.isEmpty()) { |
58 |
| -if (stack2.size() ==1) { |
59 |
| -stack1.push(stack2.pop()); |
60 |
| -break; |
| 18 | +if (s.charAt(i) =='(' ||s.charAt(i) =='+' ||s.charAt(i) =='-') { |
| 19 | +stack.addLast(s.charAt(i) +""); |
| 20 | + }elseif (Character.isDigit(s.charAt(i))) { |
| 21 | +intstart =i; |
| 22 | +while (i <s.length() &&Character.isDigit(s.charAt(i))) { |
| 23 | +i++; |
| 24 | + } |
| 25 | +stack.addLast(s.substring(start,i)); |
| 26 | +i--; |
| 27 | + }elseif (s.charAt(i) ==')') { |
| 28 | +intresult =0; |
| 29 | +while (!stack.isEmpty() && !stack.peekLast().equals("(")) { |
| 30 | +StringnumStr =stack.pollLast(); |
| 31 | +intnumInt =Integer.parseInt(numStr); |
| 32 | +if (!stack.isEmpty() && (stack.peekLast().equals("-") ||stack.peekLast().equals("+"))) { |
| 33 | +Stringoperator =stack.pollLast(); |
| 34 | +if (operator.equals("+")) { |
| 35 | +result +=numInt; |
| 36 | + }elseif (operator.equals("-")) { |
| 37 | +result -=numInt; |
| 38 | + } |
| 39 | + }else { |
| 40 | +result +=numInt; |
| 41 | +if (!stack.isEmpty() &&stack.peekLast().equals("(")) { |
| 42 | +stack.pollLast(); |
| 43 | +break; |
| 44 | + } |
| 45 | + } |
61 | 46 | }
|
62 |
| -intoperand1 =Integer.parseInt(stack2.pop()); |
63 |
| -Stringoperator =stack2.pop(); |
64 |
| -intoperand2 =Integer.parseInt(stack2.pop()); |
65 |
| -if (operator.equals("+")) { |
66 |
| -exp =operand1 +operand2; |
67 |
| - }elseif (operator.equals("-")) { |
68 |
| -exp =operand1 -operand2; |
| 47 | +if (!stack.isEmpty() &&stack.peekLast().equals("(")) { |
| 48 | +stack.pollLast(); |
69 | 49 | }
|
70 |
| -stack2.push(String.valueOf(exp)); |
| 50 | +stack.addLast(result +""); |
71 | 51 | }
|
72 |
| -i++; |
73 | 52 | }
|
74 | 53 | }
|
75 |
| - |
76 |
| -if (stack1.size() ==1) { |
77 |
| -returnInteger.parseInt(stack1.pop()); |
78 |
| - } |
79 |
| - |
80 |
| -while (!stack1.isEmpty()) { |
81 |
| -stack2.push(stack1.pop()); |
82 |
| - } |
83 |
| -while (!stack2.isEmpty()) { |
84 |
| -if (stack2.size() ==1) { |
85 |
| -stack1.push(stack2.pop()); |
86 |
| -break; |
87 |
| - } |
88 |
| -intexp =0; |
89 |
| -intoperand1 =Integer.parseInt(stack2.pop()); |
90 |
| -Stringoperator =stack2.pop(); |
91 |
| -intoperand2 =Integer.parseInt(stack2.pop()); |
92 |
| -if (operator.equals("+")) { |
93 |
| -exp =operand1 +operand2; |
94 |
| - }elseif (operator.equals("-")) { |
95 |
| -exp =operand1 -operand2; |
| 54 | +intresult =0; |
| 55 | +while (!stack.isEmpty() &&stack.peekLast() !="(") { |
| 56 | +StringnumStr =stack.pollLast(); |
| 57 | +intnumInt =Integer.parseInt(numStr); |
| 58 | +if (!stack.isEmpty()) { |
| 59 | +Stringoperator =stack.pollLast(); |
| 60 | +if (operator.equals("+")) { |
| 61 | +result +=numInt; |
| 62 | + }elseif (operator.equals("-")) { |
| 63 | +result -=numInt; |
| 64 | + } |
| 65 | + }else { |
| 66 | +result +=numInt; |
96 | 67 | }
|
97 |
| -stack2.push(String.valueOf(exp)); |
98 | 68 | }
|
99 |
| -returnInteger.parseInt(stack1.pop()); |
| 69 | +return!stack.isEmpty() ?Integer.parseInt(stack.peekLast()) +result :result; |
100 | 70 | }
|
| 71 | + |
101 | 72 | }
|
102 | 73 |
|
103 | 74 | }
|