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

Commit97725a5

Browse files
committed
Merge branch 'jktam336/150-Evaluate-Reverse-Polish-Notation' of github.com:jktam336/Neetcode-Contributions
Added JS 150-Evaluate-Reverse-Polish-Notation
2 parents85aa5c5 +53daa03 commit97725a5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*@param {string[]} tokens
3+
*@return {number}
4+
*/
5+
varevalRPN=function(tokens){
6+
letstack=[];
7+
for(consttokenoftokens){
8+
if(/^[+\-*\/]$/.test(token)){
9+
constrhs=stack.pop();
10+
constlhs=stack.pop();
11+
switch(token){
12+
case'+':
13+
stack.push(lhs+rhs);
14+
break;
15+
case'-':
16+
stack.push(lhs-rhs);
17+
break;
18+
case'*':
19+
stack.push(lhs*rhs);
20+
break;
21+
case'/':
22+
stack.push(Math.trunc(lhs/rhs));
23+
};
24+
}else{
25+
stack.push(Number(token));
26+
};
27+
};
28+
returnstack.pop();
29+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp