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

Commit18e53a3

Browse files
author
Chihung Yu
committed
more solutions
1 parent862fe46 commit18e53a3

10 files changed

+223
-33
lines changed

‎108 Convert Sorted Array to Binary Search Tree.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ var generate = function(nums, start, end){
2525
varmidIndex=start+parseInt((end-start)/2);
2626
varmidVal=nums[midIndex];
2727

28-
varleft=generate(nums,start,midIndex-1);
2928
varnode=newTreeNode(midVal);
30-
node.left=left;
31-
varright=generate(nums,midIndex+1,end);
32-
node.right=right;
29+
node.left=generate(nums,start,midIndex-1);
30+
node.right=generate(nums,midIndex+1,end);
3331

3432
returnnode;
3533
}

‎12 Integer to Roman.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Leetcode 12
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/integer-to-roman/
4+
// Author: Chihung Yu
5+
/**
6+
*@param {number} num
7+
*@return {string}
8+
*/
9+
varintToRoman=function(num){
10+
vardict=["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];
11+
varval=[1000,900,500,400,100,90,50,40,10,9,5,4,1];
12+
varresult="";
13+
14+
for(vari=0;i<val.length;i++){
15+
varv=val[i];
16+
17+
if(num>=v){
18+
varcount=parseInt(num/v);
19+
num%=v;
20+
21+
for(varj=0;j<count;j++){
22+
result=result+dict[i];
23+
}
24+
}
25+
}
26+
27+
returnresult;
28+
};

‎173 Binary Search Tree Iterator.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Definition for binary tree
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
*@constructor
11+
*@param {TreeNode} root - root of the binary search tree
12+
*/
13+
varBSTIterator=function(root){
14+
this.stack=[];
15+
this.pushLeft(root);
16+
};
17+
18+
19+
/**
20+
*@this BSTIterator
21+
*@returns {boolean} - whether we have a next smallest number
22+
*/
23+
BSTIterator.prototype.hasNext=function(){
24+
returnthis.stack.length!==0;
25+
};
26+
27+
/**
28+
*@this BSTIterator
29+
*@returns {number} - the next smallest number
30+
*/
31+
BSTIterator.prototype.next=function(){
32+
if(this.hasNext()){
33+
varnode=this.stack.pop();
34+
35+
if(node.right){
36+
this.pushLeft(node.right);
37+
}
38+
39+
returnnode.val;
40+
}
41+
};
42+
43+
BSTIterator.prototype.pushLeft=function(node){
44+
while(node){
45+
this.stack.push(node);
46+
node=node.left;
47+
}
48+
}
49+
50+
/**
51+
* Your BSTIterator will be called like this:
52+
* var i = new BSTIterator(root), a = [];
53+
* while (i.hasNext()) a.push(i.next());
54+
*/

‎240 Search a 2D Matrix II.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*@param {number[][]} matrix
3+
*@param {number} target
4+
*@return {boolean}
5+
*/
6+
varsearchMatrix=function(matrix,target){
7+
varrow=matrix.length;
8+
9+
if(row===0){
10+
returnfalse;
11+
}
12+
13+
varcol=matrix[0].length;
14+
15+
if(col===0){
16+
returnfalse;
17+
}
18+
19+
varj=col-1;
20+
for(vari=0;i<row;i++){
21+
while(j&&matrix[i][j]>target){
22+
j--;
23+
}
24+
if(matrix[i][j]===target){
25+
returntrue;
26+
}
27+
}
28+
returnfalse;
29+
};

‎241 Different Ways to Add Parentheses.js

Whitespace-only changes.

‎35 Search Insert Position.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Leetcode 35
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/search-insert-position/
4+
// Author: Chihung Yu
15
/**
26
*@param {number[]} nums
37
*@param {number} target

‎46 Permutations.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Leetcode 309
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/permutations/
4+
// Author: Chihung Yu
5+
/**
6+
*@param {number[]} nums
7+
*@return {number[][]}
8+
*/
9+
varpermute=function(nums){
10+
varresult=[];
11+
varvisited=[];
12+
13+
generate(nums,0,visited,[],result);
14+
15+
returnresult;
16+
};
17+
18+
vargenerate=function(nums,index,visited,output,result){
19+
if(nums.length===output.length){
20+
result.push(output.slice());
21+
}
22+
23+
for(vari=0;i<nums.length;i++){
24+
if(!visited[i]){
25+
visited[i]=true;
26+
output.push(nums[i]);
27+
generate(nums,index+1,visited,output,result);
28+
output.pop();
29+
visited[i]=false;
30+
}
31+
32+
}
33+
}

‎64 Minimum Path Sum.js

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,72 @@
22
*@param {number[][]} grid
33
*@return {number}
44
*/
5-
varminPathSum=function(grid){
6-
if(grid===null||grid.length===0||grid[0].length===0){
7-
return0;
8-
}
5+
//var minPathSum = function(grid) {
6+
// if(grid === null || grid.length === 0 || grid[0].length === 0){
7+
// return 0;
8+
// }
99

10-
vardp=[[]];
10+
// var dp = [[]];
1111

12-
dp[0][0]=grid[0][0];
12+
// dp[0][0] = grid[0][0];
1313

14-
for(vari=1;i<grid.length;i++){
15-
dp.push([]);
16-
dp[i][0]=grid[i][0]+dp[i-1][0];
17-
}
14+
// for(var i = 1; i < grid.length; i++){
15+
// dp.push([]);
16+
// dp[i][0] = grid[i][0] + dp[i - 1][0];
17+
// }
18+
19+
// for(var j = 1; j < grid[0].length; j++){
20+
// dp[0][j] = grid[0][j] + dp[0][j - 1];
21+
// }
22+
23+
// for(i = 1; i < grid.length; i++){
24+
// for(j = 1; j < grid[i].length; j++){
25+
// var val1 = dp[i - 1][j];
26+
// var val2 = dp[i][j - 1];
27+
28+
// dp[i][j] = Math.min(val1, val2) + grid[i][j];
29+
// }
30+
// }
31+
32+
// return dp[dp.length - 1][dp[0].length - 1];
33+
// }
34+
35+
36+
/**
37+
*@param {number[][]} triangle
38+
*@return {number}
39+
*/
40+
41+
// use only O(n) space
42+
varminimumTotal=function(triangle){
43+
varpreResult=[];
44+
varrows=triangle.length;
1845

19-
for(varj=1;j<grid[0].length;j++){
20-
dp[0][j]=grid[0][j]+dp[0][j-1];
46+
if(rows>0){
47+
preResult.push(triangle[0][0]);
48+
}else{
49+
returnnull;
2150
}
2251

23-
for(i=1;i<grid.length;i++){
24-
for(j=1;j<grid[i].length;j++){
25-
varval1=dp[i-1][j];
26-
varval2=dp[i][j-1];
52+
for(varrow=1;row<rows;row++){
53+
varcurResult=[];
54+
55+
for(varcol=0;col<triangle[row].length;col++){
56+
varval;
2757

28-
dp[i][j]=Math.min(val1,val2)+grid[i][j];
58+
if(col===0){
59+
val=preResult[col];
60+
}elseif(col===(triangle[row].length-1)){
61+
val=preResult[col-1];
62+
}else{
63+
val=Math.min(preResult[col-1],preResult[col]);
64+
}
65+
66+
curResult[col]=triangle[row][col]+val;
2967
}
68+
69+
preResult=curResult;
3070
}
3171

32-
returndp[dp.length-1][dp[0].length-1];
33-
}
72+
returnMath.min.apply(null,preResult);
73+
};

‎80 Remove Duplicates from Sorted Array II.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
*/
55
varremoveDuplicates=function(nums){
66
varn=nums.length;
7+
varindex=0;
8+
varoc=1;
79

8-
for(vari=0;i<n;i++){
9-
varcur=nums[i];
10-
if(cur===nums[i+1]){
11-
varisDup=true;
12-
varnext=i+2;
13-
14-
while(next<n&&cur===nums[next]){
15-
nums.splice(next,1);
16-
n--;
10+
for(vari=1;i<nums.length;i++){
11+
if(nums[index]===nums[i]){
12+
if(oc===2){
13+
continue;
1714
}
15+
oc++;
16+
}else{
17+
oc=1;
1818
}
19+
20+
index++;
21+
nums[index]=nums[i];
1922
}
2023

21-
returnn;
24+
returnindex+1;
2225
};

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,5 @@ By Chihung Yu
123123
93 Restore IP Addresses.<br />
124124
94 Binary Tree Inorder Traversal<br />
125125
98 Validate Binary Search Tree<br />
126+
318 Maximum Product of Word Lengths <br />
126127
README.md

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp