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

Commitfcb4c87

Browse files
committed
done 9, 111, 225, slow day
1 parent6565b77 commitfcb4c87

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

‎Easy/111-minimumDepthBinaryTree.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
*@param {TreeNode} root
10+
*@return {number}
11+
*/
12+
varminDepth=function(root){
13+
if(!root)return0;
14+
if(!root.left)returnminDepth(root.right)+1;
15+
if(!root.right)returnminDepth(root.left)+1;
16+
returnMath.min(minDepth(root.left),minDepth(root.right))+1;
17+
};

‎Easy/225-stackUsingQueues.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* this question is super easy, 80ms used
3+
*@constructor
4+
*/
5+
varStack=function(){
6+
this.stack=[];
7+
};
8+
9+
/**
10+
*@param {number} x
11+
*@returns {void}
12+
*/
13+
Stack.prototype.push=function(x){
14+
this.stack.push(x);
15+
};
16+
17+
/**
18+
*@returns {void}
19+
*/
20+
Stack.prototype.pop=function(){
21+
this.stack.pop();
22+
};
23+
24+
/**
25+
*@returns {number}
26+
*/
27+
Stack.prototype.top=function(){
28+
returnthis.stack[this.stack.length-1];
29+
};
30+
31+
/**
32+
*@returns {boolean}
33+
*/
34+
Stack.prototype.empty=function(){
35+
returnthis.stack.length===0;
36+
};

‎Easy/9-palindromeNumber.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*@param {number} x
3+
*@return {boolean}
4+
*/
5+
varisPalindrome=function(x){
6+
if(x<0)returnfalse;
7+
varrevX=0;
8+
vary=x;
9+
while(x>0){
10+
revX=revX*10+x%10;
11+
x=Math.floor(x/10);
12+
}
13+
returny===revX;
14+
};
15+
16+
// 2nd solution
17+
// it is slower than 1st solution, however, it only needs to examine half of the number
18+
// just be careful if the number is has factor 10
19+
// also a good solution in case overflow happens
20+
varisPalindrome=function(x){
21+
if(x<0||(x!==0&&x%10===0))returnfalse;
22+
varrevX=0;
23+
while(x>revX){
24+
revX=revX*10+x%10;
25+
x=Math.floor(x/10);
26+
}
27+
return(x===revX)||(x===Math.floor(revX/10));
28+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp