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

Commit3a4e0d7

Browse files
committed
add calculator
1 parent98b0580 commit3a4e0d7

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

‎Easy/235-lcaBST.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ var lowestCommonAncestor = function(root, p, q) {
2323
returnroot;
2424
};
2525

26-
// or like this?
26+
// or like this?
2727
varlowestCommonAncestor=function(root,p,q){
2828
if(p.val>root.val&&q.val>root.val){
2929
returnlowestCommonAncestor(root.right,p,q);
3030
}elseif(p.val<root.val&&q.val<root.val){
3131
returnlowestCommonAncestor(root.left,p,q);
3232
}
3333
returnroot;
34-
3534
};

‎Medium/227-calculatorII.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* !!can always use Math.trunc() to get the integer part of a number
3+
*@param {string} s
4+
*@return {number}
5+
*/
6+
varcalculate=function(s){
7+
varstack=[],
8+
i,num=0,sign='+';
9+
10+
for(i=0;i<s.length;i++){
11+
if(!isNaN(s[i])&&s[i]!==' '){
12+
num=num*10+parseInt(s[i],10);
13+
}
14+
if((isNaN(s[i]))||i===s.length-1){
15+
if(sign==='+'){
16+
stack.push(num);
17+
}elseif(sign==='-'){
18+
stack.push(-num);
19+
}elseif(sign==='*'){
20+
stack.push(parseInt(stack.pop(),10)*num);
21+
}elseif(sign==='/'){
22+
stack.push(Math.trunc(parseInt(stack.pop(),10)/num));
23+
}
24+
sign=s[i];
25+
num=0;
26+
}
27+
}
28+
29+
for(i=0;i<stack.length;i++){
30+
num+=stack[i];
31+
}
32+
33+
returnnum;
34+
};

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
*[215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) -[Solution](./Medium/215-KthLargestElementInArray.js)
167167
*[216. Combination Sum III](https://leetcode.com/problems/combination-sum-iii/) -[Solution](./Medium/216-CombinationSumIII.js)
168168
*[221. Maximal Square](https://leetcode.com/problems/maximal-square/) -[Solution](./Medium/221-maximalSquare.js)
169+
*[227. Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) -[Solution](./Medium/227-calculatorII.js)
169170
*[229. Majority Element II](https://leetcode.com/problems/majority-element-ii/) -[Solution](./Medium/229-majorityElementII.js)
170171
*[230. Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) -[Solution](./Medium/230-kthSmallestElementinBST.js)
171172
*[236. Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) -[Solution](./Medium/236-lcaBinaryTree.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp