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

Commitbf3c23d

Browse files
author
Chihung Yu
committed
add read me
1 parent18e53a3 commitbf3c23d

10 files changed

+489
-161
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
varzigzagLevelOrder=function(root){
13+
varresult=[]
14+
15+
if(!root){
16+
returnresult;
17+
}
18+
19+
varfromLeft=false;
20+
varcurLvl=[];
21+
curLvl.push(root);
22+
23+
varnextLvl=[];
24+
vartemp=[];
25+
26+
while(curLvl.length!==0){
27+
varp=curLvl.pop();
28+
temp.push(p.val);
29+
30+
if(fromLeft){
31+
if(p.left){
32+
nextLvl.push(p.left);
33+
}
34+
if(p.right){
35+
nextLvl.push(p.right);
36+
}
37+
}else{
38+
if(p.right){
39+
nextLvl.push(p.right);
40+
}
41+
if(p.left){
42+
nextLvl.push(p.left);
43+
}
44+
}
45+
46+
if(curLvl.length===0){
47+
fromLeft=!fromLeft;
48+
result.push(temp);
49+
temp=[];
50+
curLvl=nextLvl;
51+
nextLvl=[];
52+
}
53+
}
54+
55+
returnresult
56+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 {number[]} preorder
10+
*@param {number[]} inorder
11+
*@return {TreeNode}
12+
*/
13+
varbuildTree=function(preorder,inorder){
14+
if(!preorder||!inorder){
15+
returnnull;
16+
}
17+
18+
if(preorder.length!==inorder.length){
19+
returnnull;
20+
}
21+
22+
returngenerate(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
23+
};
24+
25+
26+
vargenerate=function(preorder,pl,pr,inorder,il,ir){
27+
if(pl>pr||il>ir){
28+
returnnull
29+
}
30+
31+
32+
varroot=newTreeNode(preorder[pl]);
33+
varmidIndex=-1;
34+
35+
for(vari=0;i<inorder.length;i++){
36+
if(inorder[i]===preorder[pl]){
37+
midIndex=i;
38+
break;
39+
}
40+
}
41+
42+
if(midIndex===-1){
43+
returnnull;
44+
}
45+
46+
varleft=generate(preorder,pl+1,pl+(midIndex-il),inorder,il,midIndex-1);
47+
varright=generate(preorder,pl+(midIndex-il)+1,pr,inorder,midIndex+1,ir);
48+
49+
root.left=left;
50+
root.right=right;
51+
52+
returnroot;
53+
}

‎131 Palindrome Partitioning.js

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,42 @@
1-
// Leetcode 131
2-
// Language: Javascript
3-
// Problem: https://leetcode.com/problems/palindrome-partitioning/
4-
// Author: Chihung Yu
51
/**
62
*@param {string} s
7-
*@returns {string[][]}
3+
*@return {string[][]}
84
*/
95
varpartition=function(s){
10-
116
varresult=[];
12-
varlist=[];
137

14-
if(s===null||s.length===0){
15-
return[];
16-
}
8+
generate(s,0,[],result);
179

18-
calculate(s,result,list);
1910
returnresult;
2011
};
2112

22-
varisPalindrome=function(s){
23-
vari=0;
24-
varj=s.length-1;
13+
vargenerate=function(s,index,output,result){
14+
if(index===s.length){
15+
result.push(output.slice());
16+
return;
17+
}
2518

26-
while(i<j){
27-
if(s[i]!==s[j]){
28-
returnfalse;
29-
}
19+
20+
for(vari=index;i<s.length;i++){
21+
varstr=s.substring(index,i);
3022

31-
i++;
32-
j--;
23+
if(isPalindrome(s,index,i)){
24+
output.push(s.substring(index,i+1));
25+
generate(s,i+1,output,result);
26+
output.pop();
27+
}
3328
}
34-
35-
returntrue;
3629
}
3730

38-
varcalculate=function(s,result,list){
39-
if(s.length===0){
40-
result.push(list.slice());
41-
return;
42-
}
43-
44-
for(vari=1;i<=s.length;i++){
45-
varsubstring=s.substring(0,i);
46-
47-
if(isPalindrome(substring)){
48-
list.push(substring);
49-
varrestSubstring=s.substring(i);
50-
51-
calculate(restSubstring,result,list);
52-
list.pop();
31+
varisPalindrome=function(s,head,tail){
32+
while(head<tail){
33+
if(s[head]!==s[tail]){
34+
returnfalse;
5335
}
36+
37+
head++;
38+
tail--;
5439
}
40+
41+
returntrue;
5542
}

‎200 Number of Islands.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
*@param {character[][]} grid
3+
*@return {number}
4+
*/
5+
varnumIslands=function(grid){
6+
varcount=0;
7+
8+
varrows=grid.length;
9+
10+
if(rows===0){
11+
returncount;
12+
}
13+
14+
varcols=grid[0].length;
15+
16+
if(cols===0){
17+
returncount;
18+
}
19+
20+
for(i=0;i<rows;i++){
21+
for(varj=0;j<cols;j++){
22+
if(grid[i][j]==="1"){
23+
count++;
24+
walk(grid,i,j,rows,cols);
25+
}
26+
}
27+
}
28+
returncount;
29+
};
30+
31+
varwalk=function(grid,x,y,rows,cols){
32+
if(grid[x][y]==="1"){
33+
grid[x][y]="0";
34+
}
35+
36+
if((x+1)<rows&&grid[x+1][y]==="1"){
37+
walk(grid,x+1,y,rows,cols);
38+
}
39+
40+
if((x-1)>=0&&grid[x-1][y]==="1"){
41+
walk(grid,x-1,y,rows,cols);
42+
}
43+
44+
if((y+1)<cols&&grid[x][y+1]==="1"){
45+
walk(grid,x,y+1,rows,cols);
46+
}
47+
48+
if((y-1)>=0&&grid[x][y-1]==="1"){
49+
walk(grid,x,y-1,rows,cols);
50+
}
51+
}

‎209 Minimum Size Subarray Sum.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// http://blog.csdn.net/lisonglisonglisong/article/details/45666975
2+
3+
/**
4+
*@param {number} s
5+
*@param {number[]} nums
6+
*@return {number}
7+
*/
8+
varminSubArrayLen=function(s,nums){
9+
varsum=0;
10+
varleft=0;
11+
varright=-1;
12+
varlen=nums.length;
13+
varminLen=Infinity;
14+
15+
while(right<len){
16+
while(right<len&&sum<s){
17+
sum+=nums[++right];
18+
}
19+
if(sum>=s){
20+
minLen=Math.min(right-left+1,minLen);
21+
sum-=nums[left];
22+
left++;
23+
}
24+
25+
}
26+
27+
28+
29+
returnminLen>len ?0 :minLen;
30+
};

‎31 Next Permutation.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
*@param {number[]} nums
3+
*@return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
// http://fisherlei.blogspot.com/2012/12/leetcode-next-permutation.html
6+
varnextPermutation=function(nums){
7+
varvioIndex=nums.length-1;
8+
9+
while(vioIndex>0){
10+
if(nums[vioIndex-1]<nums[vioIndex]){
11+
break;
12+
}
13+
vioIndex--;
14+
}
15+
16+
if(vioIndex>0){
17+
vioIndex--;
18+
varfirst=nums.length-1;
19+
while(first>vioIndex&&nums[first]<=nums[vioIndex]){
20+
first--;
21+
}
22+
23+
vartemp=nums[vioIndex];
24+
nums[vioIndex]=nums[first];
25+
nums[first]=temp;
26+
27+
vioIndex++;
28+
}
29+
30+
varend=nums.length-1;
31+
32+
while(end>vioIndex){
33+
temp=nums[end];
34+
nums[end]=nums[vioIndex];
35+
nums[vioIndex]=temp;
36+
37+
end--;
38+
vioIndex++;
39+
}
40+
};

‎49 Group Anagrams.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
*@param {string[]} strs
3+
*@return {string[][]}
4+
*/
5+
6+
7+
8+
vargroupAnagrams=function(strs){
9+
varhash={}
10+
11+
for(vari=0;i<strs.length;i++){
12+
varstr=strs[i];
13+
varsortedStr=sort(str);
14+
15+
hash[sortedStr]=hash[sortedStr]||[];
16+
hash[sortedStr].push(str);
17+
}
18+
19+
varresult=[];
20+
21+
for(iinhash){
22+
vararr=hash[i].sort(function(a,b){
23+
returna>b ?1 :-1;
24+
});
25+
result.push(arr);
26+
}
27+
returnresult;
28+
};
29+
30+
varsort=function(s){
31+
vararr=s.split('');
32+
arr.sort(function(a,b){
33+
returna>b ?1 :-1;
34+
});
35+
36+
returnarr.join('');
37+
}

‎50 Pow(x, n).js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*@param {number} x
3+
*@param {number} n
4+
*@return {number}
5+
*/
6+
7+
// http://blog.csdn.net/sunnyyoona/article/details/43273933
8+
varmyPow=function(x,n){
9+
if(n<0){
10+
return1.0/myPow(x,-n);
11+
}
12+
if(n===0){
13+
return1.0;
14+
}
15+
if(n===1){
16+
returnx;
17+
}
18+
19+
varsub=myPow(x,parseInt(n/2));
20+
returnsub*sub*myPow(x,n%2);
21+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp