Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit3e2387b

Browse files
fix 224 and add tests
1 parent45aa87c commit3e2387b

File tree

2 files changed

+101
-84
lines changed

2 files changed

+101
-84
lines changed
Lines changed: 55 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,74 @@
11
packagecom.fishercoder.solutions;
22

3-
importjava.util.ArrayList;
4-
importjava.util.List;
5-
importjava.util.Stack;
3+
importjava.util.Deque;
4+
importjava.util.LinkedList;
65

76
publicclass_224 {
87

98
publicstaticclassSolution1 {
10-
9+
/**
10+
* My complete original solution on 12/23/2021
11+
*/
1112
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;
2917
}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+
}
6146
}
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();
6949
}
70-
stack2.push(String.valueOf(exp));
50+
stack.addLast(result +"");
7151
}
72-
i++;
7352
}
7453
}
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;
9667
}
97-
stack2.push(String.valueOf(exp));
9868
}
99-
returnInteger.parseInt(stack1.pop());
69+
return!stack.isEmpty() ?Integer.parseInt(stack.peekLast()) +result :result;
10070
}
71+
10172
}
10273

10374
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._224;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_224Test {
10+
privatestatic_224.Solution1solution1;
11+
privatestaticintexpected;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_224.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
Strings ="1 + 1";
21+
expected =2;
22+
assertEquals(expected,solution1.calculate(s));
23+
}
24+
25+
@Test
26+
publicvoidtest2() {
27+
Strings =" 2-1 + 2 ";
28+
expected =3;
29+
assertEquals(expected,solution1.calculate(s));
30+
}
31+
32+
@Test
33+
publicvoidtest3() {
34+
Strings ="(1+(4+5+2)-3)+(6+8)";
35+
expected =23;
36+
assertEquals(expected,solution1.calculate(s));
37+
}
38+
39+
@Test
40+
publicvoidtest4() {
41+
Strings ="1-(-2)";
42+
expected =3;
43+
assertEquals(expected,solution1.calculate(s));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp