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

Commitd756a2b

Browse files
committed
160, 19, 190, 205, 219, 223
1 parentfcb4c87 commitd756a2b

7 files changed

+182
-0
lines changed

‎Easy/111-minimumDepthBinaryTree.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* }
77
*/
88
/**
9+
* very fast 111ms
910
*@param {TreeNode} root
1011
*@return {number}
1112
*/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* because there is an intersection btw two linkedlists, first align the head of longer list with
11+
* the shorter one's head. Then just keep moving the heads of both linkedlists
12+
*@param {ListNode} headA
13+
*@param {ListNode} headB
14+
*@return {ListNode}
15+
*/
16+
vargetIntersectionNode=function(headA,headB){
17+
varlenA=getLength(headA);
18+
varlenB=getLength(headB);
19+
20+
while(lenA<lenB){
21+
headB=headB.next;
22+
lenB--;
23+
}
24+
25+
while(lenB<lenA){
26+
headA=headA.next;
27+
lenA--;
28+
}
29+
30+
while(headA!==headB){
31+
headA=headA.next;
32+
headB=headB.next;
33+
}
34+
35+
returnheadA;
36+
37+
};
38+
39+
vargetLength=function(listHead){
40+
varlength=0;
41+
while(listHead){
42+
length++;
43+
listHead=listHead.next;
44+
}
45+
returnlength;
46+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* key: Two pointers, the distance of two pointer is n, when the second
10+
* pointer reaches to the end, the first pointer node is the node to be
11+
* deleted. Be careful when the second pointer reaches the end, first pointer
12+
* has not moved, then just return the second node as the new head.
13+
*
14+
*@param {ListNode} head
15+
*@param {number} n
16+
*@return {ListNode}
17+
*/
18+
varremoveNthFromEnd=function(head,n){
19+
varfHead=head,sHead=head;
20+
vardistance=0;
21+
while(distance<n){
22+
sHead=sHead.next;
23+
distance++;
24+
}
25+
if(!sHead)returnfHead.next;
26+
while(sHead.next){
27+
fHead=fHead.next;
28+
sHead=sHead.next;
29+
}
30+
fHead.next=fHead.next.next;
31+
returnhead;
32+
};

‎Easy/190-reverseBits.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* the key is to use bit AND &, after AND 1, only the last bit is reseved.
3+
*@param {number} n - a positive integer
4+
*@return {number} - a positive integer
5+
*/
6+
varreverseBits=function(n){
7+
varresult=0;
8+
for(vari=0;i<32;i++){
9+
varlastBit=n&1;
10+
result+=lastBit*Math.pow(2,31-i);
11+
n>>=1;
12+
}
13+
returnresult;
14+
};

‎Easy/205-isomorphicStrings.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* A simply solution but not efficient consider every character of ‘str1′ and
3+
* check if all occurrences of it map to same character occurrences in ‘str2′.
4+
* Time complexity of this solution is O(n*n).
5+
*
6+
* This is a better solution (NOT ACCEPTED, Time limit exceeded).
7+
* 1. check if t[i] is in the map. If yes, find out the key associated with t[i]
8+
* if key exists, but s[i] is not equal to key, return false;
9+
* if key is null, but s[i] exists in the map and map[s[i]] is not equal to t[i]
10+
* return false.
11+
*
12+
*@param {string} s
13+
*@param {string} t
14+
*@return {boolean}
15+
*/
16+
varisIsomorphic=function(s,t){
17+
varmap={};
18+
for(vari=0;i<s.length;i++){
19+
varkey=map.getKeyByValue(t[i]);
20+
if(key&&s[i]!==key){
21+
returnfalse;
22+
}elseif(s[i]inmap){
23+
if(map[s[i]]!==t[i]){
24+
returnfalse;
25+
}
26+
}else{
27+
map[s[i]]=t[i];
28+
}
29+
}
30+
returntrue;
31+
};
32+
33+
Object.prototype.getKeyByValue=function(value){
34+
for(varpropinthis){
35+
if(this.hasOwnProperty(prop)){
36+
if(this[prop]===value)
37+
returnprop;
38+
}
39+
}
40+
};

‎Easy/219-containsDuplicateII.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* key: use JavaScript Object to keep track the num and its index
3+
* REMEBER: use if (key in Obj) to check if the Obj has the key 'key'
4+
*@param {number[]} nums
5+
*@param {number} k
6+
*@return {boolean}
7+
*/
8+
varcontainsNearbyDuplicate=function(nums,k){
9+
varnumIndex={};
10+
for(vari=0;i<nums.length;i++){
11+
if(nums[i]innumIndex){
12+
if(i-numIndex[nums[i]]<=k){
13+
returntrue;
14+
}
15+
}
16+
numIndex[nums[i]]=i;
17+
}
18+
19+
returnfalse;
20+
};

‎Easy/223-rectangleArea.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Key: find the left most x, right most x, top most y, top most y,
3+
* that is the overlap rectangle.
4+
*@param {number} A
5+
*@param {number} B
6+
*@param {number} C
7+
*@param {number} D
8+
*@param {number} E
9+
*@param {number} F
10+
*@param {number} G
11+
*@param {number} H
12+
*@return {number}
13+
*/
14+
varcomputeArea=function(A,B,C,D,E,F,G,H){
15+
varareaA=(C-A)*(D-B);
16+
varareaB=(G-E)*(H-F);
17+
18+
varleft=Math.max(A,E);
19+
varright=Math.min(C,G);
20+
vartop=Math.min(D,H);
21+
varbottom=Math.max(B,F);
22+
23+
varoverlapArea=0;
24+
if(right>left&&top>bottom){
25+
overlapArea=(right-left)*(top-bottom);
26+
}
27+
28+
returnareaA+areaB-overlapArea;
29+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp