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

Commit3e16c29

Browse files
author
Chihung Yu
committed
add more
1 parenta12c922 commit3e16c29

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

‎148 Sort List.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
13+
varsortList=function(head){
14+
if(head===null){
15+
returnnull;
16+
}
17+
18+
varlen=0;
19+
varp=head;
20+
21+
while(p){
22+
len++;
23+
p=p.next;
24+
}
25+
26+
varnewHead=sort(len);
27+
28+
returnnewHead;
29+
30+
functionsort(len){
31+
if(len===1){
32+
vartemp=head;
33+
head=head.next;
34+
temp.next=null;
35+
returntemp;
36+
}
37+
38+
varleftHead=sort(parseInt(len/2));
39+
varrightHead=sort(len-parseInt(len/2));
40+
varnewHead=merge(leftHead,rightHead);
41+
42+
returnnewHead;
43+
}
44+
45+
functionmerge(first,second){
46+
varh=newListNode(-1);
47+
varcur=h;
48+
49+
while(first&&second){
50+
51+
52+
if(first.val<=second.val){
53+
cur.next=first;
54+
first=first.next;
55+
}else{
56+
cur.next=second;
57+
second=second.next;
58+
}
59+
60+
cur=cur.next;
61+
}
62+
63+
if(first){
64+
cur.next=first;
65+
}
66+
67+
if(second){
68+
cur.next=second;
69+
}
70+
71+
cur=h.next;
72+
h.next=null;
73+
returncur;
74+
}
75+
};

‎219 Contains Duplicate II.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ var containsNearbyDuplicate = function(nums, k) {
3434
}
3535

3636
returnfalse;
37-
};
37+
};
38+
39+

‎220 Contains Duplicate III.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} k
4+
*@param {number} t
5+
*@return {boolean}
6+
*/
7+
8+
varsearch=function(pairs,t,k){
9+
varp1=0;
10+
11+
while(p1<pairs.length){
12+
varp2=p1+1;
13+
14+
while(p2<pairs.length){
15+
if(Math.abs(pairs[p1][0]-pairs[p2][0])<=t&&Math.abs(pairs[p1][1]-pairs[p2][1])<=k){
16+
returntrue;
17+
}else{
18+
if(Math.abs(pairs[p1][0]-pairs[p2][0])>t){
19+
break;
20+
}else{
21+
p2++;
22+
}
23+
}
24+
}
25+
26+
p1++;
27+
}
28+
29+
returnfalse;
30+
}
31+
32+
varcontainsNearbyAlmostDuplicate=function(nums,k,t){
33+
if(k<=0){
34+
returnfalse;
35+
}
36+
37+
if(nums===null||nums.length===0){
38+
returnfalse;
39+
}
40+
41+
varpairs=[];
42+
43+
for(vari=0;i<nums.length;i++){
44+
pairs.push([nums[i],i]);
45+
}
46+
47+
pairs.sort(function(a,b){returna[0]>b[0] ?1 :-1;});
48+
49+
returnsearch(pairs,t,k);
50+
};

‎233 Number of Digit One.js

Whitespace-only changes.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp