Skip to content

Commit c3e84bb

Browse files
add 224
1 parent 6ce3870 commit c3e84bb

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ LeetCode
200200
|0221|[Maximal Square](https://leetcode.com/problems/maximal-square/) | c | [c++](./src/0221-Maximal-Square/0221.cpp) |[python](./src/0221-Maximal-Square/0221.py)|[go](./src/0221-Maximal-Square/0221.go)|[js](./src/0221-Maximal-Square/0221.js)|Medium|
201201
|0222|[Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/) | c | [c++](./src/0222-Count-Complete-Tree-Nodes/0222.cpp) |[python](./src/0222-Count-Complete-Tree-Nodes/0222.py)|||Medium|
202202
|0223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/) | c | [c++](./src/0223-Rectangle-Area/0223.cpp) |[python](./src/0223-Rectangle-Area/0223.py)|||Medium|
203-
|0224|[Basic Calculator](https://leetcode.com/problems/rectangle-area/) | c | [c++](./src/0224-Basic-Calculator/0224.cpp) |[python](./src/0224-Basic-Calculator/0224.py)|[go](./src/0224-Basic-Calculator/0224.go)|[js](./src/0224-Basic-Calculator/0224.js)|Hard|
203+
|0224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/) | c | [c++](./src/0224-Basic-Calculator/0224.cpp) |[python](./src/0224-Basic-Calculator/0224.py)|[go](./src/0224-Basic-Calculator/0224.go)|[js](./src/0224-Basic-Calculator/0224.js)|Hard|
204204
|0226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | c | [c++](./src/0226-Invert-Binary-Tree/0226.cpp) |[python](./src/0226-Invert-Binary-Tree/0226.py)|||Easy|
205205
|0228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/) | c | [c++](./src/0228-Summary-Ranges/0228.cpp) |[python](./src/0228-Summary-Ranges/0228.py)|||Medium|
206206
|0229|[Majority Element II](https://leetcode.com/problems/majority-element-ii/) | c | [c++](./src/0229-Majority-Element-II/0229.cpp) |[python](./src/0229-Majority-Element-II/0229.py)|||Medium|

src/0224-Basic-Calculator/0224.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution
2+
{
3+
public:
4+
int calculate(string s)
5+
{
6+
vector<int> st;
7+
int op = 1, r = 0, l = 0;
8+
for (auto c : s)
9+
{
10+
if (c >= '0' && c <= '9') r = r * 10 + (c - '0');
11+
else if (c == '+' || c == '-')
12+
{
13+
l += op * r;
14+
r = 0, op = c == '+' ? 1 : -1;
15+
}
16+
else if (c == '(')
17+
{
18+
st.push_back(l);
19+
st.push_back(op);
20+
op = 1, l = 0;
21+
}
22+
else if (c == ')')
23+
{
24+
l += op * r;
25+
r = 0;
26+
int tp = st.back(); st.pop_back();
27+
l = tp * l + st.back(); st.pop_back();
28+
}
29+
}
30+
return l + op * r;
31+
}
32+
};

src/0224-Basic-Calculator/0224.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func calculate(s string) int {
2+
st := make([]int, 0)
3+
op, l, r := 1, 0, 0
4+
for _, c := range s {
5+
if c >= '0' && c <= '9' {
6+
r = r * 10 + (int(c) - 48)
7+
} else if c == '+' || c == '-' {
8+
l += op * r
9+
r = 0
10+
if c == '+' {
11+
op = 1
12+
} else {
13+
op = -1
14+
}
15+
} else if c == '(' {
16+
st = append(st, l)
17+
st = append(st, op)
18+
op, l = 1, 0
19+
} else if c == ')' {
20+
l += op * r
21+
r = 0
22+
l = st[len(st) - 1] * l + st[len(st) - 2]
23+
st = st[:len(st) - 2]
24+
}
25+
}
26+
return l + op * r
27+
}

src/0224-Basic-Calculator/0224.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var calculate = function(s) {
2+
let st = [], op = 1, r = l = 0;
3+
for (let c of s) {
4+
if (c >= '0' && c <= '9') r = r * 10 + Number(c);
5+
else if (c == '+' || c == '-') {
6+
l += op * r;
7+
r = 0, op = c == '+' ? 1 : -1;
8+
} else if (c == '(') {
9+
st.push(l), st.push(op);
10+
op = 1, l = 0;
11+
} else if (c == ')') {
12+
l += op * r;
13+
r = 0;
14+
l = st.pop() * l + st.pop();
15+
}
16+
}
17+
return l + op * r;
18+
};

src/0224-Basic-Calculator/0224.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def calculate(self, s):
3+
st, op, r, l = [], 1, 0, 0
4+
for c in s:
5+
if c.isdigit():
6+
r = r * 10 + int(c)
7+
elif c in "+-":
8+
l += op * r
9+
r, op = 0, 1 if c == '+' else -1
10+
elif c == "(":
11+
st.append(l)
12+
st.append(op)
13+
op, l = 1, 0
14+
elif c == ")":
15+
l += op * r
16+
r = 0
17+
l = st.pop() * l + st.pop()
18+
return l + op * r

0 commit comments

Comments
 (0)