Skip to content

Commit eb520c6

Browse files
author
Botao Xiao
committed
[Function add]
1. Add leetcode solutions.
1 parent f6e8d65 commit eb520c6

File tree

3 files changed

+76
-27
lines changed

3 files changed

+76
-27
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@
391391

392392
[223. Rectangle Area](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/223.%20Rectangle%20Area.md)
393393

394+
[224. Basic Calculator](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/224.%20Basic%20Calculator.md)
395+
396+
[225. Implement Stack using Queues](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/225.%20Implement%20Stack%20using%20Queues.md)
397+
394398
## Algorithm(4th_Edition)
395399
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
396400
All java realization codes are placed in different packages.

leetcode/224. Basic Calculator.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
1-
## 223. Rectangle Area
2-
3-
### Question
4-
Find the total area covered by two rectilinear rectangles in a 2D plane.
5-
6-
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
7-
![question](https://leetcode.com/static/images/problemset/rectangle_area.png)
8-
```
9-
Example:
10-
11-
Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2
12-
Output: 45
13-
14-
```
15-
16-
### Thinking:
17-
* Method 1:两个长方形的面积和 - 交集的面积
18-
a## 224. Basic Calculator
1+
## 224. Basic Calculator
192

203
### Question
214
Implement a basic calculator to evaluate a simple expression string.
@@ -81,16 +64,39 @@ class Solution {
8164
}
8265
}
8366
```
67+
68+
### 二刷
69+
1. Refer to previous answer.
8470
```Java
8571
class Solution {
86-
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
87-
int total = (C - A) * (D - B) + (G - E) * (H - F);
88-
if(E > C || G < A || F > D || H < B) return total;
89-
else{
90-
int height = Math.min(H, D) - Math.max(B, F);
91-
int width = Math.min(G, C) - Math.max(A, E);
92-
return total - height * width;
72+
public int calculate(String s) {
73+
char[] arr = s.toCharArray();
74+
Stack<Integer> stack = new Stack<>();
75+
int sign = 1;
76+
int result = 0;
77+
int len = arr.length;
78+
for(int i = 0; i < arr.length; i++){
79+
char c = arr[i];
80+
if(Character.isDigit(c)){
81+
int num = c - '0';
82+
while(i + 1 < len && Character.isDigit(arr[i + 1]))
83+
num = num * 10 + (arr[++i] - '0');
84+
result += num * sign;
85+
}else if(c == '+'){
86+
sign = 1;
87+
}else if(c == '-'){
88+
sign = -1;
89+
}else if(c == '('){
90+
stack.push(result);
91+
stack.push(sign);
92+
result = 0;
93+
sign = 1;
94+
}else if(c == ')'){
95+
sign = stack.pop();
96+
result = stack.pop() + sign * result;
97+
}
9398
}
99+
return result;
94100
}
95101
}
96-
```
102+
```

leetcode/225. Implement Stack using Queues.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,43 @@ class MyStack {
6363
* int param_3 = obj.top();
6464
* boolean param_4 = obj.empty();
6565
*/
66-
```
66+
```
67+
68+
### 二刷
69+
1. Use LinkedList to realize stack.
70+
```Java
71+
class MyStack {
72+
LinkedList<Integer> stack = null;
73+
int size = 0;
74+
/** Initialize your data structure here. */
75+
public MyStack() {
76+
stack = new LinkedList<>();
77+
}
78+
/** Push element x onto stack. */
79+
public void push(int x) {
80+
this.stack.addFirst(x);
81+
this.size++;
82+
}
83+
/** Removes the element on top of the stack and returns that element. */
84+
public int pop() {
85+
this.size --;
86+
return this.stack.pollFirst();
87+
}
88+
/** Get the top element. */
89+
public int top() {
90+
return this.stack.get(0);
91+
}
92+
/** Returns whether the stack is empty. */
93+
public boolean empty() {
94+
return this.size == 0;
95+
}
96+
}
97+
/**
98+
* Your MyStack object will be instantiated and called as such:
99+
* MyStack obj = new MyStack();
100+
* obj.push(x);
101+
* int param_2 = obj.pop();
102+
* int param_3 = obj.top();
103+
* boolean param_4 = obj.empty();
104+
*/
105+
```

0 commit comments

Comments
 (0)