8000 update 224 · githubniraj/Leetcode@76ee1ef · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 76ee1ef

Browse files
update 224
1 parent 21ff909 commit 76ee1ef

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/main/java/com/fishercoder/solutions/_224.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,53 @@ private int cal(String s) {
109109
}
110110
}
111111

112+
public static class Solution3 {
113+
/**
114+
* A more elegant solution using stack and iterative approach, credit: https://leetcode.com/problems/basic-calculator/solutions/62361/iterative-java-solution-with-stack/
115+
* Key points:
116+
* 1. use an integer to represent sign: 1 or -1, so it can be pushed onto a stack that's of Integer type;
117+
*/
118+
public int calculate(String s) {
119+
Deque<Integer> stack = new LinkedList<>();
120+
int result = 0;
121+
int sign = 1;
122+
int num = 0;
123+
for (int i = 0; i < s.length(); i++) {
124+
char c = s.charAt(i);
125+
if (Character.isDigit(c)) {
126+
num = num * 10 + c - '0';
127+
} else if (c == '(') {
128+
//we push the result onto the stack first, then sign
129+
stack.addLast(result);
130+
stack.addLast(sign);
131+
132+
//reset them
133+
sign = 1;
134+
num = 0;
135+
} else if (c == ')') {
136+
//this means we reached the end of one parenthesis, so we compute result and reset num
137+
result += num * sign;
138+
num = 0;
139+
140+
result *= stack.pollLast();//this is the last sign we pushed onto the stack
141+
result += stack.pollLast();//this is the last number on the stack
142+
} else if (c == '+') {
143+
result += num * sign;
144+
//reset below two variables
145+
num = 0;
146+
sign = 1;
147+
} else if (c == '-') {
148+
result -= num * sign;
149+
//reset below two variables
150+
num = 0;
151+
sign = 1;
152+
}
153+
}
154+
if (num != 0) {
155+
result += num * sign;
156+
}
157+
return result;
158+
}
159+
}
160+
112161
}

src/test/java/com/fishercoder/_224Test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
public class _224Test {
1010
private static _224.Solution1 solution1;
1111
private static _224.Solution2 solution2;
12+
private static _224.Solution3 solution3;
1213
private static int expected;
1314

1415
@BeforeEach
1516
public void setup() {
1617
solution1 = new _224.Solution1();
1718
solution2 = new _224.Solution2();
19+
solution3 = new _224.Solution3();
1820
}
1921

2022
@Test
@@ -39,6 +41,7 @@ public void test3() {
3941
expected = 23;
4042
assertEquals(expected, solution1.calculate(s));
4143
assertEquals(expected, solution2.calculate(s));
44+
assertEquals(expected, solution3.calculate(s));
4245
}
4346

4447
@Test

0 commit comments

Comments
 (0)
0