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

Commit3069801

Browse files
committed
first commit
0 parents  commit3069801

24 files changed

+606
-0
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+

‎Easy/100-sameTree.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
if(p===null&&q===null)returntrue;
15+
if(p!==null&&q!==null&&p.val===q.val){
16+
if(isSameTree(p.left,q.left)&&isSameTree(p.right,q.right)){
17+
returntrue;
18+
}
19+
}
20+
21+
returnfalse;
22+
};

‎Easy/104-maxDepth.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+
// not accepted!!
13+
varmaxDepth=function(root){
14+
if(root===null){return0;}
15+
if(maxDepth(root.left)>maxDepth(root.right)){
16+
root=root.left;
17+
}else{
18+
root=root.right;
19+
}
20+
returnmaxDepth(root)+1;
21+
};
22+
23+
// accepted
24+
varmaxDepth=function(root){
25+
varmax=0;
26+
varlmax=maxDepth(root.left);
27+
varrmax=maxDepth(root.right);
28+
if(lmax>rmax){
29+
root=root.left;
30+
max=lmax;
31+
}else{
32+
root=root.right;
33+
max=rmax;
34+
}
35+
returnmax+1;
36+
}

‎Easy/109-majorityElement.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*@param {number[]} nums
3+
*@return {number}
4+
*/
5+
// accepted, but not good.
6+
varmajorityElement=function(nums){
7+
varappears={};
8+
for(vari=0;i<nums.length;i++){
9+
if(appears[nums[i]]){
10+
appears[nums[i]]++;
11+
}else{
12+
appears[nums[i]]=1;
13+
}
14+
}
15+
16+
for(keyinappears){
17+
if(appears[key]>=nums.length/2){
18+
returnparseInt(key);
19+
}
20+
}
21+
};
22+
23+
// better solution
24+
varmajorityElement=function(nums){
25+
nums.sort();
26+
varmid=Math.floor(nums.length/2);
27+
returnnums[mid];
28+
};

‎Easy/191-numerOneBits.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*@param {number} n - a positive integer
3+
*@return {number}
4+
*/
5+
varhammingWeight=function(n){
6+
varcounts=0;
7+
while(n>0){
8+
if(n&1===1){
9+
counts++;
10+
}
11+
// be careful to use >>> instead of >>
12+
// in JavaScript, >> is used for signed numbers, >>> is used for unsigned numbers
13+
n>>>=1;
14+
}
15+
returncounts;
16+
};

‎Easy/202-happyNumber.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*@param {number} n
3+
*@return {boolean}
4+
*/
5+
varisHappy=function(n){
6+
varsquareSums=[n];
7+
while(n!==1){
8+
n=squareSum(n);
9+
// check this square sum num exists or not.
10+
varisSquareSumApeared=squareSums.indexOf(n)>-1;
11+
if(isSquareSumApeared)returnfalse;
12+
squareSums.push(n);
13+
}
14+
returntrue;
15+
};
16+
17+
varsquareSum=function(n){
18+
varsum=0;
19+
while(n>0){
20+
varlastDigit=n%10;
21+
sum+=lastDigit*lastDigit;
22+
n=Math.floor(n/10);
23+
}
24+
returnsum;
25+
}

‎Easy/206-reversedLinkedList.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
*@param {ListNode} head
10+
*@return {ListNode}
11+
*/
12+
// Iterative way
13+
varreverseList=function(head){
14+
if(head===null||head.next===null){returnhead;}
15+
varheadNext=head.next;
16+
head.next=null;
17+
while(head!==null&&headNext!==null){
18+
// keep a reference of the headNext's next node
19+
vartmp=headNext.next;
20+
headNext.next=head;
21+
head=headNext;
22+
headNext=tmp;
23+
}
24+
25+
returnhead;
26+
};
27+
28+
// recursive way
29+
varreverseList=function(head){
30+
if(head===null||head.next===null){returnhead;}
31+
varheadNext=head.next;
32+
head.next=null;
33+
varnextReversedListHead=reverseList(headNext);
34+
// now headNext is the last node of the reversed List on the right of head!!!
35+
headNext.next=head;
36+
37+
returnnextReversedListHead;
38+
};

‎Easy/21-mergeSortedLists.js‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
*@param {ListNode} l1
10+
*@param {ListNode} l2
11+
*@return {ListNode}
12+
*/
13+
varmergeTwoLists=function(l1,l2){
14+
varl3=newListNode();
15+
varl3Head=l3;
16+
if(l1===null&&l2===null){
17+
returnnull;
18+
}
19+
while(true){
20+
if(l1===null&&l2!==null){
21+
l3.val=l2.val;
22+
l2=l2.next;
23+
}elseif(l2===null&&l1!==null){
24+
l3.val=l1.val;
25+
l1=l1.next;
26+
}elseif(l1.val<=l2.val){
27+
l3.val=l1.val;
28+
l1=l1.next;
29+
}else{
30+
l3.val=l2.val;
31+
l2=l2.next;
32+
}
33+
if(l1===null&&l2===null){
34+
break;
35+
}
36+
l3.next=newListNode();
37+
l3=l3.next;
38+
}
39+
40+
returnl3Head;
41+
};
42+
43+
// discard first dummy node in the new list which contains the merged result.
44+
varmergeTwoLists=function(l1,l2){
45+
varl3=newListNode();
46+
varl3Head=l3;
47+
while(true){
48+
if(l1===null&&l2===null){
49+
break;
50+
}
51+
if(l1===null&&l2!==null){
52+
l3.next=l2;
53+
break;
54+
}
55+
if(l2===null&&l1!==null){
56+
l3.next=l1;
57+
break;
58+
}
59+
if(l1.val<=l2.val){
60+
l3.next=l1;
61+
l1=l1.next;
62+
}else{
63+
l3.next=l2;
64+
l2=l2.next;
65+
}
66+
l3=l3.next;
67+
}
68+
69+
returnl3Head.next;
70+
};

‎Easy/217-containsDuplicate.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*@param {number[]} nums
3+
*@return {boolean}
4+
*/
5+
varcontainsDuplicate=function(nums){
6+
varappears={};
7+
varcontainsDuplicate=false;
8+
for(vari=0;i<nums.length;i++){
9+
if(appears[nums[i]]){
10+
appears[nums[i]]+=1;
11+
containsDuplicate=true;
12+
break;
13+
}else{
14+
appears[nums[i]]=1;
15+
}
16+
}
17+
18+
returncontainsDuplicate;
19+
};

‎Easy/226-invertBinaryTree.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+
*@return {TreeNode}
11+
*/
12+
varinvertTree=function(root){
13+
if(root===null){returnnull;}
14+
varnode=root.left;
15+
root.left=root.right;
16+
root.right=node;
17+
if(root.left){invertTree(root.left);}
18+
if(root.right){invertTree(root.right);}
19+
returnroot;
20+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp