Skip to content

Commit 45625cb

Browse files
committed
Stack
Stack
1 parent 0a06ad7 commit 45625cb

File tree

5 files changed

+116
-1
lines changed

5 files changed

+116
-1
lines changed

.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="LinkedList"/>
4+
<classpathentry kind="src" path="Stack"/>
45
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
56
<classpathentry kind="output" path="bin"/>
67
</classpath>

Queue/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The codes here briefly summarizes my work on queue.
1+
The java codes here basically summarize my work on Queue. The codes are driven by CMU course 08-722 Data Structures for Application Programmers and some leetcode problems related about Queue.
22

33
# MovingAverage.java
44
## 346. Moving Average from Data Stream

LinkedList/SnakeGame.java Queue/SnakeGame.java

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public static void main(String[] args) {
130130
System.out.println(obj.move("U"));
131131
System.out.println(obj.move("R"));
132132
System.out.println(obj.move("D"));
133+
133134
}
134135
}
135136

Stack/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The java codes here basically summarize my work on Stack. The codes are driven by CMU course 08-722 Data Structures for Application Programmers and some LeetCode problems related about Stack.
2+
3+
# Solution.java
4+
## 224. Basic Calculator
5+
Implement a basic calculator to evaluate a simple expression string.
6+
7+
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
8+
9+
You may assume that the given expression is always valid.
10+
11+
Some examples:
12+
~~~~
13+
"1 + 1" = 2
14+
" 2-1 + 2 " = 3
15+
"(1+(4+5+2)-3)+(6+8)" = 23
16+
~~~~
17+
Note: Do not use the eval built-in library function.
18+
19+
#### Solution
20+
1. First remove spaces of the string s.
21+
2. Use stack to handle parentheses, pop the stack when encountering ')' until popping out '('.
22+
3. Evaluate the string without parentheses using calculateHelper.
23+
24+
Be careful with '-', for example, "2-(5-6)".

Stack/Solution.java

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.util.Stack;
2+
3+
public class Solution {
4+
public int calculate(String s) {
5+
if (s == null || s.length() == 0) {
6+
return 0; // how to handle the corner case?
7+
}
8+
9+
s = s.replaceAll("\\s","");
10+
if (!s.contains("(") && !s.contains(")")) {
11+
return calculateHelper(s);
12+
}
13+
14+
int pos = 0;
15+
int len = s.length();
16+
Stack<String> stack = new Stack<String>();
17+
while (pos < len) {
18+
if (s.charAt(pos) == ')') {
19+
StringBuilder sb = new StringBuilder();
20+
String prev = stack.pop();
21+
while (!prev.equals("(")) {
22+
sb.insert(0, prev);
23+
prev = stack.pop();
24+
}
25+
int ans = calculateHelper(sb.toString());
26+
stack.push(Integer.toString(ans));
27+
}
28+
else {
29+
stack.push(String.valueOf(s.charAt(pos)));
30+
}
31+
pos++;
32+
}
33+
34+
StringBuilder sb = new StringBuilder();
35+
while (!stack.isEmpty()) {
36+
String str = stack.pop();
37+
sb.insert(0, str);
38+
}
39+
40+
return calculateHelper(sb.toString());
41+
}
42+
43+
private int calculateHelper(String s) {
44+
int index = 0;
45+
int len = s.length();
46+
int ans = 0;
47+
char operator = '+';
48+
while (index < len) {
49+
StringBuilder sb = new StringBuilder();
50+
if (s.charAt(index) == '-') {
51+
operator = operator == '+' ? '-' : '+';
52+
index++;
53+
}
54+
while (index < len && s.charAt(index) != '+' && s.charAt(index) != '-') {
55+
sb.append(s.charAt(index++));
56+
}
57+
58+
if (operator == '+') {
59+
ans += Integer.parseInt(sb.toString());
60+
}
61+
else {
62+
ans -= Integer.parseInt(sb.toString());
63+
}
64+
if (index < len) {
65+
operator = s.charAt(index++);
66+
}
67+
}
68+
return ans;
69+
}
70+
71+
public static void main(String[] args) {
72+
Solution s = new Solution();
73+
String x = "2-(5-6)";
74+
System.out.println(x);
75+
System.out.println(s.calculate(x));
76+
}
77+
78+
/*
79+
test case:
80+
""
81+
" "
82+
"0"
83+
"1 + 1"
84+
" 2-1 + 2 "
85+
"(1+(4+5+2)-3)+(6+8)"
86+
"(1+(4+5+2)-3)+((6 + 8) + (6+8))"
87+
"2-(5-6)"
88+
*/
89+
}

0 commit comments

Comments
 (0)