Skip to content

Commit 9893495

Browse files
committed
feat: leetcode 224
1 parent 7cf38a7 commit 9893495

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

assets/data/category.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,12 @@
16411641
"path": "../../problemset/min-stack/README.md",
16421642
"difficulty": "简单"
16431643
},
1644+
{
1645+
"id": 224,
1646+
"title": "基本计算器",
1647+
"path": "../../problemset/basic-calculator/README.md",
1648+
"difficulty": "困难"
1649+
},
16441650
{
16451651
"id": 2104,
16461652
"title": "子数组范围和",

assets/data/problems.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,16 @@
19891989
"url": "https://leetcode-cn.com/problems/rectangle-area/",
19901990
"path": "../../problemset/rectangle-area/README.md"
19911991
},
1992+
{
1993+
"id": 224,
1994+
"title": {
1995+
"cn": "基本计算器",
1996+
"en": "basic-calculator"
1997+
},
1998+
"difficulty": "困难",
1999+
"url": "https://leetcode-cn.com/problems/basic-calculator/",
2000+
"path": "../../problemset/basic-calculator/README.md"
2001+
},
19922002
{
19932003
"id": 258,
19942004
"title": {

assets/docs/CATEGORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
| 98. [验证二叉搜索树](../../problemset/validate-binary-search-tree/README.md) | 中等 |
324324
| 150. [逆波兰表达式求值](../../problemset/valuate-reverse-polish-notation/README.md) | 中等 |
325325
| 155. [最小栈](../../problemset/min-stack/README.md) | 简单 |
326+
| 224. [基本计算器](../../problemset/basic-calculator/README.md) | 困难 |
326327
| 2104. [子数组范围和](../../problemset/sum-of-subarray-ranges/README.md) | 中等 |
327328

328329
## 递归 & 迭代

assets/docs/PROBLEMS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@
398398

399399
[223. 矩形面积](../../problemset/rectangle-area/README.md)
400400

401+
[224. 基本计算器](../../problemset/basic-calculator/README.md)
402+
401403
[258. 各位相加](../../problemset/add-digits/README.md)
402404

403405
[300. 最长递增子序列](../../problemset/longest-increasing-subsequence/README.md)

problemset/basic-calculator/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# 基本计算器
2+
3+
> 难度:困难
4+
>
5+
> https://leetcode-cn.com/problems/basic-calculator/
6+
7+
## 题目
8+
9+
给你一个字符串表达式 `s` ,请你实现一个基本计算器来计算并返回它的值。
10+
11+
注意:不允许使用任何将字符串作为数学表达式计算的内置函数,比如 `eval()`
12+
13+
 
14+
### 示例
15+
16+
#### 示例 1:
17+
18+
```
19+
输入:s = "1 + 1"
20+
输出:2
21+
```
22+
23+
#### 示例 2:
24+
25+
```
26+
输入:s = " 2-1 + 2 "
27+
输出:3
28+
```
29+
30+
#### 示例 3:
31+
32+
```
33+
输入:s = "(1+(4+5+2)-3)+(6+8)"
34+
输出:23
35+
```
36+
37+
## 解题
38+
39+
```ts
40+
/**
41+
* 括号展开 + 栈
42+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
43+
* @param s
44+
* @returns
45+
*/
46+
export function calculate(s: string): number {
47+
const stack = [1]
48+
let sign = 1
49+
50+
let result = 0
51+
const len = s.length
52+
let i = 0
53+
54+
while (i < len) {
55+
switch (s[i]) {
56+
case ' ':
57+
i++
58+
break
59+
case '+':
60+
sign = stack[stack.length - 1]
61+
i++
62+
break
63+
case '-':
64+
sign = -stack[stack.length - 1]
65+
i++
66+
break
67+
case '(':
68+
stack.push(sign)
69+
i++
70+
break
71+
case ')':
72+
stack.pop()
73+
i++
74+
break
75+
default:
76+
numberHandler()
77+
}
78+
}
79+
80+
function numberHandler() {
81+
let num = 0
82+
while (i < len && !(isNaN(Number(s[i]))) && s[i] !== ' ') {
83+
num = num * 10 + s[i].charCodeAt(0) - '0'.charCodeAt(0)
84+
i++
85+
}
86+
result += sign * num
87+
}
88+
89+
return result
90+
}
91+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { calculate } from '.'
3+
4+
describe('基本计算器', () => {
5+
testCase(calculate)
6+
})
7+
8+
function testCase(fn: (s: string) => number) {
9+
it.each([
10+
['1 + 1', 2],
11+
[' 2-1 + 2 ', 3],
12+
['(1+(4+5+2)-3)+(6+8)', 23],
13+
])('示例 %#', (s, expected) => {
14+
expect(fn(s)).toBe(expected)
15+
})
16+
}

problemset/basic-calculator/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 括号展开 + 栈
3+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
4+
* @param s
5+
* @returns
6+
*/
7+
export function calculate(s: string): number {
8+
const stack = [1]
9+
let sign = 1
10+
11+
let result = 0
12+
const len = s.length
13+
let i = 0
14+
15+
while (i < len) {
16+
switch (s[i]) {
17+
case ' ':
18+
i++
19+
break
20+
case '+':
21+
sign = stack[stack.length - 1]
22+
i++
23+
break
24+
case '-':
25+
sign = -stack[stack.length - 1]
26+
i++
27+
break
28+
case '(':
29+
stack.push(sign)
30+
i++
31+
break
32+
case ')':
33+
stack.pop()
34+
i++
35+
break
36+
default:
37+
numberHandler()
38+
}
39+
}
40+
41+
function numberHandler() {
42+
let num = 0
43+
while (i < len && !(isNaN(Number(s[i]))) && s[i] !== ' ') {
44+
num = num * 10 + s[i].charCodeAt(0) - '0'.charCodeAt(0)
45+
i++
46+
}
47+
result += sign * num
48+
}
49+
50+
return result
51+
}

0 commit comments

Comments
 (0)