Skip to content

Commit 4c46203

Browse files
author
haotf
committed
feat: 逆波兰表达式
1 parent 5205538 commit 4c46203

File tree

4 files changed

+1570
-1
lines changed

4 files changed

+1570
-1
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
2-
log
2+
log
3+
node_modules

Diff for: 150.逆波兰表达式rpn.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const isOperator = (s) => ["+", "-", "*", "/"].includes(s);
2+
const parse = (expression) => {
3+
const tokens = [];
4+
for (let i = 0; i < expression.length; i++) {
5+
let token = "";
6+
if (isOperator(expression[i]) && isOperator(tokens[tokens.length - 1])) {
7+
token = expression[i];
8+
i++;
9+
}
10+
while (/\d/.test(expression[i])) {
11+
token = token + expression[i];
12+
i++;
13+
}
14+
if (token) {
15+
tokens.push(Number(token));
16+
i--;
17+
} else {
18+
if (expression[i] !== " ") {
19+
tokens.push(expression[i]);
20+
}
21+
}
22+
}
23+
return tokens;
24+
};
25+
26+
const transform = (tokens) => {
27+
const operators = [];
28+
const result = [];
29+
for (let i = 0; i < tokens.length; i++) {
30+
const ele = tokens[i];
31+
if (typeof ele === "number") {
32+
result.push(ele);
33+
} else {
34+
if (ele === "*" || ele === "/" || ele === "(") {
35+
operators.push(ele);
36+
} else if (operators[operators.length - 1] === "(") {
37+
operators.push(ele);
38+
} else if (ele === ")") {
39+
while (operators[operators.length - 1] !== "(") {
40+
result.push(operators.pop());
41+
}
42+
operators.pop();
43+
} else {
44+
while (operators.length > 0) {
45+
result.push(operators.pop());
46+
}
47+
operators.push(ele);
48+
}
49+
}
50+
}
51+
while (operators.length > 0) {
52+
result.push(operators.pop());
53+
}
54+
return result;
55+
};
56+
57+
const calc = (tokens) => {
58+
let stack = [];
59+
for (let i = 0; i < tokens.length; i++) {
60+
const ele = tokens[i];
61+
if (typeof ele === "number") {
62+
stack.push(ele);
63+
} else {
64+
const right = stack.pop();
65+
const left = stack.pop();
66+
switch (ele) {
67+
case "+":
68+
stack.push(left + right);
69+
break;
70+
case "-":
71+
stack.push(left - right);
72+
break;
73+
case "*":
74+
stack.push(left * right);
75+
break;
76+
case "/":
77+
stack.push(left / right);
78+
break;
79+
}
80+
}
81+
}
82+
return stack[0];
83+
};
84+
const rpn = (expression) => {
85+
const tokens = parse(expression);
86+
const result = transform(tokens);
87+
const value = calc(result);
88+
console.log(value);
89+
return value;
90+
};
91+
92+
rpn("12 + (7 - 3) * 2 + 9 / 3");
93+
94+
rpn("((10 * (6 * ((9 + 3) * 11))) + 17) + 50");
95+
96+
rpn("2 + 30 * (20 - 10) / (100 / 5)");

Diff for: package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "leetcode",
3+
"version": "1.0.0",
4+
"description": "leecode",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/coderhaotf/leetcode.git"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"bugs": {
15+
"url": "https://github.com/coderhaotf/leetcode/issues"
16+
},
17+
"homepage": "https://github.com/coderhaotf/leetcode#readme",
18+
"devDependencies": {
19+
"cz-conventional-changelog": "^3.3.0"
20+
},
21+
"config": {
22+
"commitizen": {
23+
"path": "./node_modules/cz-conventional-changelog"
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)