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

Commit3121d3d

Browse files
committed
add and modifed some solutions
1 parentdbf7b50 commit3121d3d

22 files changed

+687
-54
lines changed

‎127 Word Ladder.js

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,77 @@ var ladderLength = function(beginWord, endWord, wordList) {
6565
}
6666

6767
return0;
68-
};
68+
};
69+
70+
// will time exceeded. javascript hash is slower than set
71+
varladderLength=function(beginWord,endWord,wordList){
72+
if(beginWord===endWord){
73+
return0;
74+
}
75+
76+
varqueue=[];
77+
varvisited={};
78+
varcount=1;
79+
varbaseCharCode='a'.charCodeAt(0);
80+
81+
queue.push(beginWord);
82+
83+
while(queue.length){
84+
varlen=queue.length;
85+
86+
for(vari=0;i<len;i++){
87+
varword=queue.shift();
88+
89+
for(varj=0;j<word.length;j++){
90+
for(vark=0;k<26;k++){
91+
varnewChar=String.fromCharCode(baseCharCode+k);
92+
varnewWord=word.substring(0,j)+newChar+word.substring(j+1);
93+
94+
if(newWord===endWord){
95+
returncount+1;
96+
}
97+
98+
if(!visited[newWord]&&wordList.has(newWord)){
99+
visited[newWord]=true;
100+
queue.push(newWord);
101+
}
102+
}
103+
}
104+
}
105+
106+
count++;
107+
}
108+
109+
return0;
110+
};
111+
112+
113+
114+
115+
HiThiago
116+
117+
IverymuchappreciatethatyoutookthetimewritingthiswarmwelcomingletterandprovidedmetheopportunitytocomeonsitevisitingtheteamatPeriscope.
118+
Aftermuchthought,I've decided to accept offer at another company. It was really a tough call for me since I really like the product, role and people I met during my visit.
119+
Again,Icannotthankyouenoughforyourtime,andsupport.It's been a great pleasure to know you and the team. I hope that we cross paths in the near future.
120+
121+
Wishyou,teams,andPeriscopeallthesuccess.
122+
123+
Regards,
124+
Jerry
125+
126+
127+
128+
129+
HiCynthia
130+
131+
Thankyourforpatienceandsupportalongtheway.
132+
IverymuchappreciatethatyoutookthetimeansweringmanyofmyquestionsaboutthePeriscope,androle.
133+
134+
Aftermuchthought,I've decided to accept offer at another company. It was really a tough call for me since I really like the product and people I met during my visit.
135+
136+
Again,Icannotthankyouenoughforyourtime,andsupport.It's been a great pleasure to know you and the team. I hope that we cross paths in the near future.
137+
138+
Wishyou,teams,andPeriscopeallthesuccess.
139+
140+
Regards,
141+
Jerry

‎128 Longest Consecutive Sequence.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
2+
3+
// For example,
4+
// Given [100, 4, 200, 1, 3, 2],
5+
// The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
6+
7+
// Your algorithm should run in O(n) complexity.
8+
9+
// Hide Company Tags Google Facebook
10+
// Hide Tags Array Union Find
11+
// Hide Similar Problems (M) Binary Tree Longest Consecutive Sequence
12+
13+
14+
/**
15+
*@param {number[]} nums
16+
*@return {number}
17+
*/
18+
varlongestConsecutive=function(nums){
19+
varmaxLen=-Infinity;
20+
varhash={};
21+
22+
for(vari=0;i<nums.length;i++){
23+
hash[nums[i]]=1;
24+
}
25+
26+
varvisited={};
27+
28+
for(i=0;i<nums.length;i++){
29+
varval=nums[i];
30+
if(visited[val]){
31+
continue;
32+
}
33+
visited[val]=true;
34+
varlen=1;
35+
varpreVal=val-1;
36+
while(hash[preVal]){
37+
len++
38+
visited[preVal--]=true;
39+
}
40+
varnxtVal=val+1;
41+
while(hash[nxtVal]){
42+
len++
43+
visited[nxtVal++]=true;
44+
}
45+
46+
if(len>maxLen){
47+
maxLen=len;
48+
}
49+
}
50+
51+
returnmaxLen;
52+
};

‎146 LRU Cache.js

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,116 @@ LRUCache.prototype.set = function(key, value) {
9191
}
9292

9393
this.map.set(key,newNode);
94-
};
94+
};
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
// Second Implementation
106+
107+
108+
functionDoublyLinkListNode(key,value){
109+
this.key=key;
110+
this.value=value;
111+
this.prev=this.next=null;
112+
}
113+
114+
/**
115+
*@constructor
116+
*/
117+
varLRUCache=function(capacity){
118+
this.head=this.tail=null;
119+
this.maxCapacity=capacity;
120+
this.currSize=0;
121+
this.hash={};
122+
};
123+
124+
/**
125+
*@param {number} key
126+
*@returns {number}
127+
*/
128+
LRUCache.prototype.get=function(key){
129+
if(!this.hash[key]){
130+
return-1;
131+
}
132+
133+
this.moveToHead(key);
134+
returnthis.hash[key].value;
135+
};
136+
137+
/**
138+
*@param {number} key
139+
*@param {number} value
140+
*@returns {void}
141+
*/
142+
LRUCache.prototype.set=function(key,value){
143+
if(this.maxCapacity<=0){
144+
return;
145+
}
146+
147+
if(!this.hash[key]){
148+
149+
if(this.currSize===this.maxCapacity){
150+
this.removeLast();
151+
this.currSize--;
152+
}
153+
154+
this.hash[key]=newDoublyLinkListNode(key,value);
155+
this.currSize++;
156+
}
157+
158+
this.hash[key].value=value;
159+
this.moveToHead(key);
160+
};
161+
162+
LRUCache.prototype.removeLast=function(){
163+
if(this.tail===null){
164+
return;
165+
}
166+
167+
deletethis.hash[this.tail.key];
168+
varnewTail=this.tail.prev;
169+
170+
if(newTail===null){
171+
this.head=this.tail=null;
172+
return;
173+
}
174+
175+
this.tail.prev=null;
176+
newTail.next=null;
177+
this.tail=newTail;
178+
}
179+
180+
LRUCache.prototype.moveToHead=function(key){
181+
varnewHead=this.hash[key];
182+
183+
if(this.head===null&&this.tail===null){
184+
this.head=this.tail=newHead;
185+
}
186+
187+
if(newHead===this.head){
188+
return;
189+
}
190+
191+
if(newHead===this.tail){
192+
this.tail=newHead.prev;
193+
}
194+
195+
if(newHead.prev){
196+
newHead.prev.next=newHead.next;
197+
}
198+
if(newHead.next){
199+
newHead.next.prev=newHead.prev;
200+
}
201+
202+
newHead.prev=null;
203+
newHead.next=this.head;
204+
this.head.prev=newHead;
205+
this.head=newHead;
206+
}

‎157 Read N Characters Given Read4.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ var solution = function(read4) {
5454

5555
returntotal;
5656
};
57-
};
57+
};
58+
59+
60+
// [tricky] [important]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// The API: int read4(char *buf) reads 4 characters at a time from a file.
2+
3+
// The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
4+
5+
// By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
6+
7+
// Note:
8+
// The read function may be called multiple times.
9+
10+
// Hide Company Tags Bloomberg Google Facebook
11+
// Hide Tags String
12+
// Hide Similar Problems (E) Read N Characters Given Read4
13+
14+
15+
16+
/**
17+
* Definition for read4()
18+
*
19+
*@param {character[]} buf Destination buffer
20+
*@return {number} The number of characters read
21+
* read4 = function(buf) {
22+
* ...
23+
* };
24+
*/
25+
26+
/**
27+
*@param {function} read4()
28+
*@return {function}
29+
*/
30+
varsolution=function(read4){
31+
letbufRead=[];
32+
letcount=0;// how many characters read with read4
33+
leti=0;
34+
35+
/**
36+
*@param {character[]} buf Destination buffer
37+
*@param {number} n Maximum number of characters to read
38+
*@return {number} The number of characters read
39+
*/
40+
returnfunction(buf,n){
41+
letnumChrRead=0;
42+
43+
while(numChrRead<n){
44+
if(i===0){
45+
count=read4(bufRead);
46+
}
47+
48+
while(i<count&&numChrRead<n){
49+
buf[numChrRead++]=bufRead[i++];
50+
}
51+
52+
// read4 buffer used up, start over
53+
if(i===count){
54+
i=0;
55+
}
56+
57+
// end of file
58+
if(count<4){
59+
break;
60+
}
61+
}
62+
63+
returnnumChrRead;
64+
};
65+
};

‎20 Valid Parentheses.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,26 @@ var isValid = function(s) {
2727
index++;
2828
}
2929

30+
returnstack.length===0;
31+
};
32+
33+
// second attempt
34+
35+
varisValid=function(s){
36+
varstack=[];
37+
38+
for(vari=0;i<s.length;i++){
39+
varchr=s[i];
40+
41+
if(chr==='('||chr==='{'||chr==='['){
42+
stack.push(chr);
43+
}elseif(chr===')'||chr==='}'||chr===']'){
44+
vartop=stack.pop();
45+
if(!top||(top==='('&&chr!==')')||(top==='{'&&chr!=='}')||(top==='['&&chr!==']')){
46+
returnfalse;
47+
}
48+
}
49+
}
50+
3051
returnstack.length===0;
3152
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp