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

Commiteba61e8

Browse files
committed
add 14, 203, 234, 38, 67
1 parentf304527 commiteba61e8

File tree

7 files changed

+163
-3
lines changed

7 files changed

+163
-3
lines changed

‎Easy/14-longestCommonPrefix.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*@param {string[]} strs
3+
*@return {string}
4+
*/
5+
varlongestCommonPrefix=function(strs){
6+
if(strs.length===0)return'';
7+
varfirstString=strs[0];
8+
varcommonLength=firstString.length;
9+
for(vari=1;i<strs.length;i++){
10+
for(varj=0;j<commonLength;j++){
11+
if(!strs[i][j])break;
12+
if(firstString[j]!==strs[i][j])break;
13+
}
14+
commonLength=j;
15+
}
16+
17+
returnfirstString.substring(0,commonLength);
18+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* Key: set a prev pointer to track the previous node
10+
* if the first node has the value val, delete it first,
11+
* then move head to next head.
12+
*
13+
*@param {ListNode} head
14+
*@param {number} val
15+
*@return {ListNode}
16+
*/
17+
varremoveElements=function(head,val){
18+
if(!head)returnnull;
19+
while(head&&head.val===val)head=head.next;
20+
varheadCopy=head;
21+
varprev=head;
22+
while(head){
23+
head=head.next;
24+
if(head&&head.val===val)prev.next=head.next;
25+
elseprev=head;
26+
}
27+
28+
returnheadCopy;
29+
};

‎Easy/206-reversedLinkedList.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
// Iterative way
1313
varreverseList=function(head){
14-
if(head===null||head.next===null){returnhead;}
14+
if(head===null||head.next===null)returnhead;
1515
varheadNext=head.next;
1616
head.next=null;
1717
while(head!==null&&headNext!==null){
@@ -27,7 +27,7 @@ var reverseList = function(head) {
2727

2828
// recursive way
2929
varreverseList=function(head){
30-
if(head===null||head.next===null){returnhead;}
30+
if(head===null||head.next===null)returnhead;
3131
varheadNext=head.next;
3232
head.next=null;
3333
varnextReversedListHead=reverseList(headNext);

‎Easy/234-palindromeLinkedList.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* Key: find the middle of the list first
10+
* reverse the second half and then compare it with first half
11+
*@param {ListNode} head
12+
*@return {boolean}
13+
*/
14+
varisPalindrome=function(head){
15+
if(!head||!head.next)returntrue;
16+
varfastHead=head;
17+
varslowHead=head;
18+
while(fastHead.next&&fastHead.next.next){
19+
slowHead=slowHead.next;
20+
fastHead=fastHead.next.next;
21+
}
22+
23+
// reverse the scond half
24+
varcenter=slowHead.next;
25+
varcenterNext=center.next;
26+
slowHead.next=null;
27+
center.next=null;
28+
while(centerNext){
29+
vartmp=centerNext.next;
30+
centerNext.next=center;
31+
center=centerNext;
32+
centerNext=tmp;
33+
}
34+
35+
while(head&&center){
36+
if(head.val!==center.val)returnfalse;
37+
head=head.next;
38+
center=center.next;
39+
}
40+
41+
returntrue;
42+
};

‎Easy/299-bullsandCows.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ var getHint = function(secret, guess) {
2121
elsesecretMap[secret[i]]=1;
2222
}
2323
for(varj=0;j<guess.length;j++){
24-
console.log(secretMap[guess[j]]);
2524
if(guess[j]!==secret[j]&&secretMap[guess[j]]){
2625
cow++;
2726
secretMap[guess[j]]--;

‎Easy/38-countandSay.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Key: find the number of occurrences of the number and place the occurrences before the number
3+
* Be careful about the last element.
4+
*
5+
*@param {number} n
6+
*@return {string}
7+
*/
8+
varcountAndSay=function(n){
9+
varinput='1';
10+
varresult=input;
11+
for(vari=1;i<n;i++){
12+
result='';
13+
varcount=1;
14+
for(varj=0;j<input.length;j++){
15+
if(j===input.length-1){
16+
result=result+count+input[j];
17+
break;
18+
}
19+
if(input[j]===input[j+1]){
20+
count++;
21+
}else{
22+
result=result+count+input[j];
23+
count=1;
24+
}
25+
}
26+
input=result;
27+
}
28+
returnresult;
29+
};

‎Easy/67-addBinary.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
*@param {string} a
3+
*@param {string} b
4+
*@return {string}
5+
*/
6+
varaddBinary=function(a,b){
7+
varextra=0;
8+
varresult=[];
9+
varaLength=a.length;
10+
varbLength=b.length;
11+
vari=0;
12+
if(aLength<=bLength){
13+
i=bLength-1;
14+
for(varj=0;j<bLength-aLength;j++){
15+
a='0'+a;
16+
}
17+
}else{
18+
i=aLength-1;
19+
for(varj=0;j<aLength-bLength;j++){
20+
b='0'+b;
21+
}
22+
}
23+
24+
while(i>=0){
25+
if(extra===0){
26+
result[i]=a[i]^b[i];
27+
if(a[i]==='1'&&b[i]==='1'){
28+
extra=1;
29+
}
30+
}elseif(extra===1){
31+
if(a[i]==='0'&&b[i]==='0'){
32+
extra=0;
33+
result[i]='1';
34+
}else{
35+
result[i]=a[i]&b[i];
36+
}
37+
}
38+
i--;
39+
}
40+
if(extra===1)result.unshift('1');
41+
42+
returnresult.join('');
43+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp