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

Commit6565b77

Browse files
committed
02-10 list
1 parentb7fc26c commit6565b77

File tree

7 files changed

+142
-0
lines changed

7 files changed

+142
-0
lines changed

‎Easy/102-binaryTreeLevelOrder.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
varlevelOrder=function(root){
13+
if(!root)return[];
14+
varpreLevel=[root],currLevel=[root];
15+
varresult=[[root.val]];
16+
while(true){
17+
if(currLevel.length===0)break;
18+
varval=[];
19+
currLevel=[];
20+
for(vari=0;i<preLevel.length;i++){
21+
if(preLevel[i].left){
22+
currLevel.push(preLevel[i].left);
23+
val.push(preLevel[i].left.val);
24+
}
25+
if(preLevel[i].right){
26+
currLevel.push(preLevel[i].right);
27+
val.push(preLevel[i].right.val);
28+
}
29+
}
30+
preLevel=[];
31+
preLevel=currLevel;
32+
if(val.length>0)result.push(val);
33+
}
34+
35+
returnresult;
36+
};

‎Easy/112-pathSum.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
*@param {number} sum
11+
*@return {boolean}
12+
*/
13+
// recursion
14+
varhasPathSum=function(root,sum){
15+
if(!root)returnfalse;
16+
if(root.val===sum&&!root.left&&!root.right)returntrue;
17+
if(hasPathSum(root.left,sum-root.val))returntrue;
18+
if(hasPathSum(root.right,sum-root.val))returntrue;
19+
returnfalse;
20+
};

‎Easy/118-pascalTriangle.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*@param {number} numRows
3+
*@return {number[][]}
4+
*/
5+
vargenerate=function(numRows){
6+
if(numRows===0)return[];
7+
varresult=[[1]];
8+
for(vari=1;i<numRows;i++){
9+
varpreRow=result[i-1];
10+
varnewRow=[1];
11+
for(varj=1;j<i;j++){
12+
newRow[j]=preRow[j-1]+preRow[j];
13+
}
14+
newRow.push(1);
15+
result.push(newRow);
16+
}
17+
returnresult;
18+
};

‎Easy/119-pascalTriangleII.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* calculate the element in reverse, resultRow[i] += resultRow[i-1]
3+
* we don't have to keep resultRow[i] if we do it from back to start.
4+
*@param {number} rowIndex
5+
*@return {number[]}
6+
*/
7+
vargetRow=function(rowIndex){
8+
varresultRow=[1];
9+
for(vari=1;i<=rowIndex;i++){
10+
for(varj=i-1;j>0;j--){
11+
resultRow[j]+=resultRow[j-1];
12+
}
13+
resultRow.push(1);
14+
}
15+
16+
returnresultRow;
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*@param {number} n
3+
*@return {number}
4+
*/
5+
// the trick is to count 5s in prime factors
6+
vartrailingZeroes=function(n){
7+
varnums=0;
8+
varfactor=5;
9+
while(n>=factor){
10+
nums+=Math.floor(n/factor);
11+
factor*=5;
12+
}
13+
14+
returnnums;
15+
};

‎Easy/198-houseRobber.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ var rob = function(nums) {
1414

1515
returnmaxAmount.pop();
1616
};
17+
18+
// O(1) space, O(N) time
19+
varrob=function(nums){
20+
if(nums.length===0)return0;
21+
varprevMax=0;
22+
// the currMax is the money the robber has robbed when the robber arrives at ith house,
23+
// the robber has not robber the ith house yet.
24+
varcurrMax=nums[0];
25+
for(vari=2;i<=nums.length;i++){
26+
vartmp=currMax;
27+
currMax=Math.max(currMax,(prevMax+nums[i-1]));
28+
prevMax=tmp;
29+
}
30+
31+
returncurrMax;
32+
};

‎Easy/66-plusOne.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*@param {number[]} digits
3+
*@return {number[]}
4+
*/
5+
varplusOne=function(digits){
6+
varlength=digits.length;
7+
varindex=length-1;
8+
while(index>=0){
9+
if(++digits[index]<10)break;
10+
digits[index]-=10;
11+
if(index===0){
12+
digits.unshift(1);
13+
break;
14+
}
15+
index--;
16+
}
17+
18+
returndigits;
19+
20+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp