Skip to content

Commit 67cbbd1

Browse files
committed
Java solution 224 && 225
1 parent 46bee31 commit 67cbbd1

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

java/_224BasicCalculator.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Implement a basic calculator to evaluate a simple expression string.
5+
* The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
6+
* You may assume that the given expression is always valid.
7+
* <p>
8+
* Some examples:
9+
* "1 + 1" = 2
10+
* " 2-1 + 2 " = 3
11+
* "(1+(4+5+2)-3)+(6+8)" = 23
12+
* <p>
13+
* Note: Do not use the eval built-in library function.
14+
* <p>
15+
* Created by drfish on 6/8/2017.
16+
*/
17+
public class _224BasicCalculator {
18+
19+
public int calculate(String s) {
20+
int sign = 1;
21+
int result = 0;
22+
Stack<Integer> stack = new Stack<>();
23+
24+
for (int i = 0; i < s.length(); i++) {
25+
if (Character.isDigit(s.charAt(i))) {
26+
int sum = s.charAt(i) - '0';
27+
while (i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))) {
28+
sum = sum * 10 + s.charAt(i + 1) - '0';
29+
i++;
30+
}
31+
result += sum * sign;
32+
} else if (s.charAt(i) == '+') {
33+
sign = 1;
34+
} else if (s.charAt(i) == '-') {
35+
sign = -1;
36+
} else if (s.charAt(i) == '(') {
37+
stack.push(result);
38+
stack.push(sign);
39+
result = 0;
40+
sign = 1;
41+
} else if (s.charAt(i) == ')') {
42+
result = result * stack.pop() + stack.pop();
43+
}
44+
}
45+
return result;
46+
}
47+
48+
public static void main(String[] args) {
49+
_224BasicCalculator solution = new _224BasicCalculator();
50+
assert 2 == solution.calculate("1 + 1");
51+
assert 3 == solution.calculate(" 2-1 + 2 ");
52+
assert 23 == solution.calculate("(1+(4+5+2)-3)+(6+8)");
53+
}
54+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
/**
5+
* Implement the following operations of a stack using queues.
6+
* <p>
7+
* push(x) -- Push element x onto stack.
8+
* pop() -- Removes the element on top of the stack.
9+
* top() -- Get the top element.
10+
* empty() -- Return whether the stack is empty.
11+
* <p>
12+
* Notes:
13+
* You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
14+
* Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
15+
* You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
16+
* <p>
17+
* Credits:
18+
* Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
19+
* <p>
20+
* Created by drfish on 6/8/2017.
21+
*/
22+
public class _225ImplementStackUsingQueues {
23+
public class MyStack {
24+
private Queue<Integer> queue;
25+
26+
/**
27+
* Initialize your data structure here.
28+
*/
29+
public MyStack() {
30+
queue = new LinkedList<>();
31+
}
32+
33+
/**
34+
* Push element x onto stack.
35+
*/
36+
public void push(int x) {
37+
queue.add(x);
38+
for (int i = 0; i < queue.size() - 1; i++) {
39+
queue.add(queue.poll());
40+
}
41+
}
42+
43+
/**
44+
* Removes the element on top of the stack and returns that element.
45+
*/
46+
public int pop() {
47+
return queue.poll();
48+
}
49+
50+
/**
51+
* Get the top element.
52+
*/
53+
public int top() {
54+
return queue.peek();
55+
}
56+
57+
/**
58+
* Returns whether the stack is empty.
59+
*/
60+
public boolean empty() {
61+
return queue.isEmpty();
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)