Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4c46203

Browse files
author
haotf
committed
feat: 逆波兰表达式
1 parent5205538 commit4c46203

File tree

4 files changed

+1570
-1
lines changed

4 files changed

+1570
-1
lines changed

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
2-
log
2+
log
3+
node_modules

‎150.逆波兰表达式rpn.js‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
constisOperator=(s)=>["+","-","*","/"].includes(s);
2+
constparse=(expression)=>{
3+
consttokens=[];
4+
for(leti=0;i<expression.length;i++){
5+
lettoken="";
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+
returntokens;
24+
};
25+
26+
consttransform=(tokens)=>{
27+
constoperators=[];
28+
constresult=[];
29+
for(leti=0;i<tokens.length;i++){
30+
constele=tokens[i];
31+
if(typeofele==="number"){
32+
result.push(ele);
33+
}else{
34+
if(ele==="*"||ele==="/"||ele==="("){
35+
operators.push(ele);
36+
}elseif(operators[operators.length-1]==="("){
37+
operators.push(ele);
38+
}elseif(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+
returnresult;
55+
};
56+
57+
constcalc=(tokens)=>{
58+
letstack=[];
59+
for(leti=0;i<tokens.length;i++){
60+
constele=tokens[i];
61+
if(typeofele==="number"){
62+
stack.push(ele);
63+
}else{
64+
constright=stack.pop();
65+
constleft=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+
returnstack[0];
83+
};
84+
constrpn=(expression)=>{
85+
consttokens=parse(expression);
86+
constresult=transform(tokens);
87+
constvalue=calc(result);
88+
console.log(value);
89+
returnvalue;
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)");

‎package.json‎

Lines changed: 26 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp