Skip to content

Commit 8cccb10

Browse files
refactor 227
1 parent d55e362 commit 8cccb10

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.ArrayDeque;
54
import java.util.Deque;
65

@@ -21,38 +20,38 @@
2120
*/
2221
public class _227 {
2322

24-
/**Credit: https://discuss.leetcode.com/topic/16935/share-my-java-solution*/
25-
public int calculate(String s) {
26-
if (s == null || s.length() == 0) {
27-
return 0;
28-
}
29-
int len = s.length();
30-
Deque<Integer> stack = new ArrayDeque<>();
31-
int num = 0;
32-
char sign = '+';
33-
for (int i = 0; i < len; i++) {
34-
if (Character.isDigit(s.charAt(i))) {
35-
num = num * 10 + s.charAt(i) - '0';
23+
public static class Solution1 {
24+
public int calculate(String s) {
25+
if (s == null || s.length() == 0) {
26+
return 0;
3627
}
37-
if ((!Character.isDigit(s.charAt(i))) && ' ' != s.charAt(i) || i == len - 1) {
38-
if (sign == '+') {
39-
stack.addLast(num);
40-
} else if (sign == '-') {
41-
stack.addLast(-num);
42-
} else if (sign == '/') {
43-
stack.addLast(stack.pollLast() / num);
44-
} else if (sign == '*') {
45-
stack.addLast(stack.pollLast() * num);
28+
int len = s.length();
29+
Deque<Integer> stack = new ArrayDeque<>();
30+
int num = 0;
31+
char sign = '+';
32+
for (int i = 0; i < len; i++) {
33+
if (Character.isDigit(s.charAt(i))) {
34+
num = num * 10 + s.charAt(i) - '0';
35+
}
36+
if ((!Character.isDigit(s.charAt(i))) && ' ' != s.charAt(i) || i == len - 1) {
37+
if (sign == '+') {
38+
stack.addLast(num);
39+
} else if (sign == '-') {
40+
stack.addLast(-num);
41+
} else if (sign == '/') {
42+
stack.addLast(stack.pollLast() / num);
43+
} else if (sign == '*') {
44+
stack.addLast(stack.pollLast() * num);
45+
}
46+
sign = s.charAt(i);
47+
num = 0;
4648
}
47-
sign = s.charAt(i);
48-
num = 0;
4949
}
50+
int result = 0;
51+
while (!stack.isEmpty()) {
52+
result += stack.poll();
53+
}
54+
return result;
5055
}
51-
int result = 0;
52-
while (!stack.isEmpty()) {
53-
result += stack.poll();
54-
}
55-
return result;
5656
}
57-
5857
}

src/test/java/com/fishercoder/_227Test.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,32 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 5/29/17.
11-
*/
129
public class _227Test {
13-
private static _227 test;
10+
private static _227.Solution1 solution1;
1411

1512
@BeforeClass
1613
public static void setup() {
17-
test = new _227();
14+
solution1 = new _227.Solution1();
1815
}
1916

2017
@Test
2118
public void test1() {
22-
assertEquals(7, test.calculate("3+2*2"));
19+
assertEquals(7, solution1.calculate("3+2*2"));
2320
}
2421

2522
@Test
2623
public void test2() {
27-
assertEquals(1, test.calculate(" 3/2 "));
24+
assertEquals(1, solution1.calculate(" 3/2 "));
2825
}
2926

3027
@Test
3128
public void test3() {
32-
assertEquals(5, test.calculate(" 3+5 / 2 "));
29+
assertEquals(5, solution1.calculate(" 3+5 / 2 "));
3330
}
3431

3532
@Test
3633
public void test4() {
37-
assertEquals(27, test.calculate("100000000/1/2/3/4/5/6/7/8/9/10"));
34+
assertEquals(27, solution1.calculate("100000000/1/2/3/4/5/6/7/8/9/10"));
3835
}
3936

4037
}

0 commit comments

Comments
 (0)