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

Commita4bd33b

Browse files
committed
add more solutions to leetcode
1 parent9bb8b90 commita4bd33b

File tree

123 files changed

+4271
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+4271
-7
lines changed

‎1 Two Sum.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} target
4+
*@return {number[]}
5+
*/
6+
vartwoSum=function(nums,target){
7+
varhash={};
8+
9+
for(vari=0;i<nums.length;i++){
10+
hash[nums[i]]=i;
11+
}
12+
13+
for(i=0;i<nums.length;i++){
14+
varnum=nums[i];
15+
vardiff=target-num;
16+
if(hash[diff]!==undefined&&hash[diff]!==i){
17+
if(hash[diff]>i){
18+
return[i+1,hash[diff]+1];
19+
}else{
20+
return[hash[diff]+1,i+1]
21+
}
22+
}
23+
}
24+
25+
return[];
26+
};

‎100 Same Tree.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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} p
10+
*@param {TreeNode} q
11+
*@return {boolean}
12+
*/
13+
varisSameTree=function(p,q){
14+
varqueue1=[];
15+
varqueue2=[];
16+
17+
queue1.push(p);
18+
queue2.push(q);
19+
20+
while(queue1.length&&queue2.length){
21+
varnode1=queue1.shift();
22+
varnode2=queue2.shift();
23+
varval1;
24+
varval2;
25+
26+
27+
if(node1===null){
28+
val1=null;
29+
}else{
30+
val1=node1.val;
31+
queue1.push(node1.left);
32+
queue1.push(node1.right);
33+
}
34+
35+
if(node2===null){
36+
val2=null;
37+
}else{
38+
val2=node2.val;
39+
queue2.push(node2.left);
40+
queue2.push(node2.right);
41+
}
42+
43+
if(val1!==val2){
44+
returnfalse;
45+
}
46+
}
47+
48+
returnqueue1.length===queue2.length;
49+
};

‎101 Symmetric Tree.js‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 {boolean}
11+
*/
12+
varisSymmetric=function(root){
13+
if(root===null){
14+
returntrue;
15+
}
16+
17+
varqueue=[];
18+
queue.push(root);
19+
20+
vartemp=[];
21+
varcurCnt=1;
22+
varnextCnt=0;
23+
24+
while(queue.length!==0){
25+
varp=queue.shift();
26+
27+
if(p!==null){
28+
temp.push(p.left);
29+
temp.push(p.right);
30+
}
31+
32+
if(queue.length===0){
33+
if(isPalindrome(temp)){
34+
queue=temp;
35+
temp=[];
36+
}else{
37+
returnfalse;
38+
}
39+
}
40+
}
41+
42+
returntrue;
43+
};
44+
45+
46+
varisPalindrome=function(arr){
47+
varhead=0;
48+
vartail=arr.length-1;
49+
50+
while(head<tail){
51+
if(arr[head]&&arr[tail]){
52+
if(arr[head].val!==arr[tail].val){
53+
returnfalse;
54+
}
55+
}elseif(arr[head]||arr[tail]){
56+
returnfalse;
57+
}
58+
59+
head++;
60+
tail--;
61+
}
62+
63+
returntrue;
64+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
varresult=[];
14+
15+
if(root===null){
16+
returnresult;
17+
}
18+
19+
varqueue=[];
20+
vartemp=[];
21+
varcurLvlCnt=1;
22+
varnextLvlCnt=0;
23+
24+
queue.push(root);
25+
26+
while(queue.length!==0){
27+
varp=queue.shift();
28+
temp.push(p.val);
29+
curLvlCnt--;
30+
31+
if(p.left){
32+
queue.push(p.left);
33+
nextLvlCnt++;
34+
}
35+
if(p.right){
36+
queue.push(p.right);
37+
nextLvlCnt++;
38+
}
39+
40+
if(curLvlCnt===0){
41+
result.push(temp);
42+
curLvlCnt=nextLvlCnt;
43+
nextLvlCnt=0;
44+
temp=[];
45+
}
46+
}
47+
48+
returnresult;
49+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
varmaxDepth=function(root){
13+
if(root===null){
14+
return0;
15+
}
16+
17+
return1+Math.max(maxDepth(root.left),maxDepth(root.right));
18+
};
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[]} inorder
10+
*@param {number[]} postorder
11+
*@return {TreeNode}
12+
*/
13+
varbuildTree=function(inorder,postorder){
14+
if(inorder===null||postorder===null){
15+
returnnull;
16+
}
17+
18+
if(inorder.length!==postorder.length){
19+
returnnull;
20+
}
21+
22+
returngenerate(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
23+
};
24+
25+
vargenerate=function(inorder,il,ir,postorder,pl,pr){
26+
if(il>ir||pl>pr){
27+
returnnull;
28+
}
29+
30+
varrootVal=postorder[pr];
31+
varroot=newTreeNode(rootVal);
32+
varrootIndex=-1;
33+
34+
for(vari=il;i<=ir;i++){
35+
varnodeVal=inorder[i];
36+
if(nodeVal===rootVal){
37+
rootIndex=i;
38+
break;
39+
}
40+
}
41+
42+
if(rootIndex===-1){
43+
returnnull;
44+
}
45+
46+
varleftTreeSize=rootIndex-il;
47+
varrightTreeSize=ir-rootIndex;
48+
49+
root.left=generate(inorder,il,rootIndex-1,postorder,pl,pl+leftTreeSize-1);
50+
root.right=generate(inorder,rootIndex+1,ir,postorder,pr-rightTreeSize,pr-1);
51+
52+
returnroot;
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
varlevelOrderBottom=function(root){
13+
varq=[];
14+
varresult=[];
15+
vartemp=[];
16+
17+
if(root===null){
18+
returnq;
19+
}
20+
21+
q.push(root);
22+
23+
varcurLvlCnt=1;
24+
varnextLvlCnt=0;
25+
26+
while(q.length!==0){
27+
varp=q.shift();
28+
curLvlCnt--;
29+
temp.push(p.val);
30+
31+
if(p.left!==null){
32+
q.push(p.left);
33+
nextLvlCnt++;
34+
}
35+
36+
if(p.right!==null){
37+
q.push(p.right);
38+
nextLvlCnt++;
39+
}
40+
41+
if(curLvlCnt===0){
42+
curLvlCnt=nextLvlCnt;
43+
nextLvlCnt=0;
44+
result.unshift(temp.slice());
45+
temp=[];
46+
}
47+
}
48+
49+
returnresult;
50+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Leetcode 108
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
4+
// Author: Chihung Yu
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val) {
8+
* this.val = val;
9+
* this.left = this.right = null;
10+
* }
11+
*/
12+
/**
13+
*@param {number[]} nums
14+
*@return {TreeNode}
15+
*/
16+
varsortedArrayToBST=function(nums){
17+
returngenerate(nums,0,nums.length-1);
18+
};
19+
20+
vargenerate=function(nums,start,end){
21+
22+
if(start>end){
23+
returnnull;
24+
}
25+
varmidIndex=start+parseInt((end-start)/2);
26+
varmidVal=nums[midIndex];
27+
28+
varleft=generate(nums,start,midIndex-1);
29+
varnode=newTreeNode(midVal);
30+
node.left=left;
31+
varright=generate(nums,midIndex+1,end);
32+
node.right=right;
33+
34+
returnnode;
35+
}

‎11 Container With Most Water.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*@param {number[]} height
3+
*@return {number}
4+
*/
5+
varmaxArea=function(height){
6+
varleft=0;
7+
varright=height.length-1;
8+
varmaxVal=0;
9+
10+
while(left<right){
11+
varcontain=(right-left)*Math.min(height[left],height[right]);
12+
maxVal=Math.max(contain,maxVal);
13+
14+
if(height[left]>=height[right]){
15+
right--;
16+
}else{
17+
left++;
18+
}
19+
}
20+
21+
returnmaxVal;
22+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp