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

Commit36f19e6

Browse files
refactor 224
1 parent7cf419e commit36f19e6

File tree

1 file changed

+92
-89
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+92
-89
lines changed

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

Lines changed: 92 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
importjava.util.Stack;
66

77
/**
8+
* 224. Basic Calculator
9+
*
810
* 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.
1313
1414
Some examples:
1515
"1 + 1" = 2
@@ -19,95 +19,98 @@ The expression string may contain open ( and closing parentheses ), the plus + o
1919
*/
2020
publicclass_224 {
2121

22-
publicintcalculate(Strings) {
23-
if (s ==null ||s.isEmpty()) {
24-
return0;
25-
}
22+
publicstaticclassSolution1 {
23+
24+
publicintcalculate(Strings) {
25+
if (s ==null ||s.isEmpty()) {
26+
return0;
27+
}
2628

27-
s =s.replaceAll("\\s","");
28-
char[]chars =s.toCharArray();
29-
List<String>filteredStr =newArrayList();
30-
for (inti =0;i <chars.length; ) {
31-
StringBuildersb =newStringBuilder();
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 =newArrayList();
32+
for (inti =0;i <chars.length; ) {
33+
StringBuildersb =newStringBuilder();
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+
}
5052

51-
for (Stringstr :filteredStr) {
52-
System.out.print(str);
53-
}
53+
for (Stringstr :filteredStr) {
54+
System.out.print(str);
55+
}
5456

55-
Stack<String>stack1 =newStack();
56-
Stack<String>stack2 =newStack();
57-
for (inti =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-
intexp =0;
68-
while (!stack2.isEmpty()) {
69-
if (stack2.size() ==1) {
70-
stack1.push(stack2.pop());
71-
break;
72-
}
73-
intoperand1 =Integer.parseInt(stack2.pop());
74-
Stringoperator =stack2.pop();
75-
intoperand2 =Integer.parseInt(stack2.pop());
76-
if (operator.equals("+")) {
77-
exp =operand1 +operand2;
78-
}elseif (operator.equals("-")) {
79-
exp =operand1 -operand2;
80-
}
81-
stack2.push(String.valueOf(exp));
82-
}
83-
i++;
84-
}
85-
}
57+
Stack<String>stack1 =newStack();
58+
Stack<String>stack2 =newStack();
59+
for (inti =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+
intexp =0;
70+
while (!stack2.isEmpty()) {
71+
if (stack2.size() ==1) {
72+
stack1.push(stack2.pop());
73+
break;
74+
}
75+
intoperand1 =Integer.parseInt(stack2.pop());
76+
Stringoperator =stack2.pop();
77+
intoperand2 =Integer.parseInt(stack2.pop());
78+
if (operator.equals("+")) {
79+
exp =operand1 +operand2;
80+
}elseif (operator.equals("-")) {
81+
exp =operand1 -operand2;
82+
}
83+
stack2.push(String.valueOf(exp));
84+
}
85+
i++;
86+
}
87+
}
8688

87-
if (stack1.size() ==1) {
88-
returnInteger.parseInt(stack1.pop());
89-
}
89+
if (stack1.size() ==1) {
90+
returnInteger.parseInt(stack1.pop());
91+
}
9092

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-
intexp =0;
100-
intoperand1 =Integer.parseInt(stack2.pop());
101-
Stringoperator =stack2.pop();
102-
intoperand2 =Integer.parseInt(stack2.pop());
103-
if (operator.equals("+")) {
104-
exp =operand1 +operand2;
105-
}elseif (operator.equals("-")) {
106-
exp =operand1 -operand2;
107-
}
108-
stack2.push(String.valueOf(exp));
109-
}
110-
returnInteger.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+
intexp =0;
102+
intoperand1 =Integer.parseInt(stack2.pop());
103+
Stringoperator =stack2.pop();
104+
intoperand2 =Integer.parseInt(stack2.pop());
105+
if (operator.equals("+")) {
106+
exp =operand1 +operand2;
107+
}elseif (operator.equals("-")) {
108+
exp =operand1 -operand2;
109+
}
110+
stack2.push(String.valueOf(exp));
111+
}
112+
returnInteger.parseInt(stack1.pop());
113+
}
114+
}
112115

113116
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp