5
5
import java .util .Stack ;
6
6
7
7
/**
8
+ * 224. Basic Calculator
9
+ *
8
10
* Implement a basic calculator to evaluate a simple expression string.
9
-
10
- The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
11
-
12
- You may assume that the given expression is always valid.
11
+ * The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
12
+ * You may assume that the given expression is always valid.
13
13
14
14
Some examples:
15
15
"1 + 1" = 2
@@ -19,95 +19,98 @@ The expression string may contain open ( and closing parentheses ), the plus + o
19
19
*/
20
20
public class _224 {
21
21
22
- public int calculate (String s ) {
23
- if (s ==null ||s .isEmpty ()) {
24
- return 0 ;
25
- }
22
+ public static class Solution1 {
23
+
24
+ public int calculate (String s ) {
25
+ if (s ==null ||s .isEmpty ()) {
26
+ return 0 ;
27
+ }
26
28
27
- s =s .replaceAll ("\\ s" ,"" );
28
- char []chars =s .toCharArray ();
29
- List <String >filteredStr =new ArrayList ();
30
- for (int i =0 ;i <chars .length ; ) {
31
- StringBuilder sb =new StringBuilder ();
32
- while (i <chars .length &&Character .isDigit (chars [i ])) {
33
- sb .append (chars [i ]);
34
- i ++;
35
- }
36
- if (i ==chars .length ) {
37
- if (sb .toString ().length () !=0 ) {
38
- filteredStr .add (sb .toString ());
39
- }
40
- }else {
41
- if (sb .toString ().length () !=0 ) {
42
- filteredStr .add (sb .toString ());
43
- }
44
- if (chars [i ] =='+' ||chars [i ] =='-' ||chars [i ] =='(' ||chars [i ] ==')' ) {
45
- filteredStr .add (String .valueOf (chars [i ]));
46
- }
47
- i ++;
48
- }
49
- }
29
+ s =s .replaceAll ("\\ s" ,"" );
30
+ char []chars =s .toCharArray ();
31
+ List <String >filteredStr =new ArrayList ();
32
+ for (int i =0 ;i <chars .length ; ) {
33
+ StringBuilder sb =new StringBuilder ();
34
+ while (i <chars .length &&Character .isDigit (chars [i ])) {
35
+ sb .append (chars [i ]);
36
+ i ++;
37
+ }
38
+ if (i ==chars .length ) {
39
+ if (sb .toString ().length () !=0 ) {
40
+ filteredStr .add (sb .toString ());
41
+ }
42
+ }else {
43
+ if (sb .toString ().length () !=0 ) {
44
+ filteredStr .add (sb .toString ());
45
+ }
46
+ if (chars [i ] =='+' ||chars [i ] =='-' ||chars [i ] =='(' ||chars [i ] ==')' ) {
47
+ filteredStr .add (String .valueOf (chars [i ]));
48
+ }
49
+ i ++;
50
+ }
51
+ }
50
52
51
- for (String str :filteredStr ) {
52
- System .out .print (str );
53
- }
53
+ for (String str :filteredStr ) {
54
+ System .out .print (str );
55
+ }
54
56
55
- Stack <String >stack1 =new Stack ();
56
- Stack <String >stack2 =new Stack ();
57
- for (int i =0 ;i <filteredStr .size (); ) {
58
- while (i <filteredStr .size () && !filteredStr .get (i ).equals (")" )) {
59
- stack1 .push (filteredStr .get (i ));
60
- i ++;
61
- }
62
- if (i !=filteredStr .size ()) {
63
- while (!stack1 .isEmpty () && !stack1 .peek ().equals ("(" )) {
64
- stack2 .push (stack1 .pop ());
65
- }
66
- stack1 .pop ();
67
- int exp =0 ;
68
- while (!stack2 .isEmpty ()) {
69
- if (stack2 .size () ==1 ) {
70
- stack1 .push (stack2 .pop ());
71
- break ;
72
- }
73
- int operand1 =Integer .parseInt (stack2 .pop ());
74
- String operator =stack2 .pop ();
75
- int operand2 =Integer .parseInt (stack2 .pop ());
76
- if (operator .equals ("+" )) {
77
- exp =operand1 +operand2 ;
78
- }else if (operator .equals ("-" )) {
79
- exp =operand1 -operand2 ;
80
- }
81
- stack2 .push (String .valueOf (exp ));
82
- }
83
- i ++;
84
- }
85
- }
57
+ Stack <String >stack1 =new Stack ();
58
+ Stack <String >stack2 =new Stack ();
59
+ for (int i =0 ;i <filteredStr .size (); ) {
60
+ while (i <filteredStr .size () && !filteredStr .get (i ).equals (")" )) {
61
+ stack1 .push (filteredStr .get (i ));
62
+ i ++;
63
+ }
64
+ if (i !=filteredStr .size ()) {
65
+ while (!stack1 .isEmpty () && !stack1 .peek ().equals ("(" )) {
66
+ stack2 .push (stack1 .pop ());
67
+ }
68
+ stack1 .pop ();
69
+ int exp =0 ;
70
+ while (!stack2 .isEmpty ()) {
71
+ if (stack2 .size () ==1 ) {
72
+ stack1 .push (stack2 .pop ());
73
+ break ;
74
+ }
75
+ int operand1 =Integer .parseInt (stack2 .pop ());
76
+ String operator =stack2 .pop ();
77
+ int operand2 =Integer .parseInt (stack2 .pop ());
78
+ if (operator .equals ("+" )) {
79
+ exp =operand1 +operand2 ;
80
+ }else if (operator .equals ("-" )) {
81
+ exp =operand1 -operand2 ;
82
+ }
83
+ stack2 .push (String .valueOf (exp ));
84
+ }
85
+ i ++;
86
+ }
87
+ }
86
88
87
- if (stack1 .size () ==1 ) {
88
- return Integer .parseInt (stack1 .pop ());
89
- }
89
+ if (stack1 .size () ==1 ) {
90
+ return Integer .parseInt (stack1 .pop ());
91
+ }
90
92
91
- while (!stack1 .isEmpty ()) {
92
- stack2 .push (stack1 .pop ());
93
- }
94
- while (!stack2 .isEmpty ()) {
95
- if (stack2 .size () ==1 ) {
96
- stack1 .push (stack2 .pop ());
97
- break ;
98
- }
99
- int exp =0 ;
100
- int operand1 =Integer .parseInt (stack2 .pop ());
101
- String operator =stack2 .pop ();
102
- int operand2 =Integer .parseInt (stack2 .pop ());
103
- if (operator .equals ("+" )) {
104
- exp =operand1 +operand2 ;
105
- }else if (operator .equals ("-" )) {
106
- exp =operand1 -operand2 ;
107
- }
108
- stack2 .push (String .valueOf (exp ));
109
- }
110
- return Integer .parseInt (stack1 .pop ());
111
- }
93
+ while (!stack1 .isEmpty ()) {
94
+ stack2 .push (stack1 .pop ());
95
+ }
96
+ while (!stack2 .isEmpty ()) {
97
+ if (stack2 .size () ==1 ) {
98
+ stack1 .push (stack2 .pop ());
99
+ break ;
100
+ }
101
+ int exp =0 ;
102
+ int operand1 =Integer .parseInt (stack2 .pop ());
103
+ String operator =stack2 .pop ();
104
+ int operand2 =Integer .parseInt (stack2 .pop ());
105
+ if (operator .equals ("+" )) {
106
+ exp =operand1 +operand2 ;
107
+ }else if (operator .equals ("-" )) {
108
+ exp =operand1 -operand2 ;
109
+ }
110
+ stack2 .push (String .valueOf (exp ));
111
+ }
112
+ return Integer .parseInt (stack1 .pop ());
113
+ }
114
+ }
112
115
113
116
}