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

Commit11839ef

Browse files
authored
Merge pull requestneetcode-gh#363 from loczek/main
Created javascript solutions for 20 problems
2 parentsce69c74 +7256e8c commit11839ef

20 files changed

+508
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
functionbuildTree(preorder,inorder){
2+
if(!preorder.length||!inorder.length)returnnull;
3+
4+
letroot=newTreeNode(preorder[0]);
5+
letmid=inorder.indexOf(preorder[0]);
6+
7+
root.left=buildTree(preorder.slice(1,mid+1),inorder.slice(0,mid));
8+
root.right=buildTree(preorder.slice(mid+1),inorder.slice(mid+1));
9+
returnroot;
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
functionpartition(s){
2+
letres=[];
3+
letpart=[];
4+
5+
functiondfs(i){
6+
if(i>=s.length){
7+
res.push(part.slice());
8+
return;
9+
}
10+
11+
for(letj=i;j<s.length;j++){
12+
if(isPali(s,i,j)){
13+
part.push(s.slice(i,j+1));
14+
dfs(j+1);
15+
part.pop();
16+
}
17+
}
18+
}
19+
20+
dfs(0);
21+
returnres;
22+
23+
functionisPali(s,l,r){
24+
while(l<r){
25+
if(s[l]!=s[r]){
26+
returnfalse;
27+
}
28+
l=l+1;
29+
r=r-1;
30+
}
31+
returntrue;
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
functiongoodNodes(root){
2+
lettotal=0;
3+
4+
functiontraverse(node,prev){
5+
if(!node)return;
6+
7+
if(node.left||node.right){
8+
traverse(node.left,Math.max(node.val,prev));
9+
traverse(node.right,Math.max(node.val,prev));
10+
}
11+
12+
if(node.val>=prev){
13+
total+=1;
14+
}
15+
}
16+
17+
traverse(root,root.val);
18+
19+
returntotal;
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
functionletterCombinations(digits){
2+
letres=[];
3+
4+
constdigitToChar={
5+
2:"abc",
6+
3:"def",
7+
4:"ghi",
8+
5:"jkl",
9+
6:"mno",
10+
7:"qprs",
11+
8:"tuv",
12+
9:"wxyz",
13+
};
14+
15+
functionbacktrack(i,curStr){
16+
if(curStr.length===digits.length){
17+
res.push(curStr);
18+
return;
19+
}
20+
21+
for(constcofdigitToChar[digits[i]]){
22+
backtrack(i+1,curStr+c);
23+
}
24+
}
25+
26+
if(digits){
27+
backtrack(0,"");
28+
}
29+
30+
returnres;
31+
}

‎javascript/198-House-Robber.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
functionrob(nums){
2+
letrob1=0;
3+
letrob2=0;
4+
5+
for(constnofnums){
6+
lettemp=Math.max(n+rob1,rob2);
7+
rob1=rob2;
8+
rob2=temp;
9+
}
10+
11+
returnrob2;
12+
}

‎javascript/2-Add-Two-Numbers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
functionaddTwoNumbers(l1,l2){
2+
lettemp1=l1;
3+
lettemp2=l2;
4+
5+
letdummy=newListNode(0);
6+
letmerged=dummy;
7+
8+
letcarry=0;
9+
10+
while(temp1||temp2){
11+
letsum=(temp1?.val||0)+(temp2?.val||0)+carry;
12+
carry=Math.floor(sum/10);
13+
sum=sum%10;
14+
15+
merged.next=newListNode(sum);
16+
merged=merged.next;
17+
18+
if(temp1)temp1=temp1?.next;
19+
if(temp2)temp2=temp2?.next;
20+
}
21+
22+
if(carry>0){
23+
merged.next=newListNode(carry);
24+
}
25+
26+
returndummy.next;
27+
}

‎javascript/202-Happy-Number.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
functionisHappy(n){
2+
constvisit=newSet();
3+
4+
while(!visit.has(n)){
5+
visit.add(n);
6+
n=sumOfSquares(n);
7+
8+
if(n==1)returntrue;
9+
}
10+
11+
returnfalse;
12+
}
13+
14+
functionsumOfSquares(n){
15+
letoutput=0;
16+
17+
while(n){
18+
letdigit=n%10;
19+
digit=digit**2;
20+
output+=digit;
21+
n=Math.floor(n/10);
22+
}
23+
24+
returnoutput;
25+
}

‎javascript/208-Implement-Trie.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
classTrieNode{
2+
constructor(){
3+
this.children={};
4+
this.endOfWord=false;
5+
}
6+
}
7+
8+
classTrie{
9+
constructor(){
10+
this.root=newTrieNode();
11+
}
12+
13+
insert(word){
14+
letcur=this.root;
15+
16+
for(constcofword){
17+
if(!(cincur.children)){
18+
cur.children[c]=newTrieNode();
19+
}
20+
cur=cur.children[c];
21+
}
22+
cur.endOfWord=true;
23+
}
24+
25+
search(word){
26+
letcur=this.root;
27+
28+
for(constcofword){
29+
if(!(cincur.children)){
30+
returnfalse;
31+
}
32+
cur=cur.children[c];
33+
}
34+
35+
returncur.endOfWord;
36+
}
37+
38+
startsWith(prefix){
39+
letcur=this.root;
40+
41+
for(constcofprefix){
42+
if(!(cincur.children)){
43+
returnfalse;
44+
}
45+
cur=cur.children[c];
46+
}
47+
48+
returntrue;
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
classTrieNode{
2+
constructor(){
3+
this.children={};
4+
this.endOfWord=false;
5+
}
6+
}
7+
8+
classWordDictionary{
9+
constructor(){
10+
this.root=newTrieNode();
11+
}
12+
13+
addWord(word){
14+
letcur=this.root;
15+
16+
for(constcofword){
17+
if(!(cincur.children)){
18+
cur.children[c]=newTrieNode();
19+
}
20+
cur=cur.children[c];
21+
}
22+
cur.endOfWord=true;
23+
}
24+
25+
search(word){
26+
functiondfs(j,root){
27+
letcur=root;
28+
29+
for(leti=j;i<word.length;i++){
30+
constc=word[i];
31+
32+
if(c==="."){
33+
for(constkeyincur.children){
34+
if(Object.prototype.hasOwnProperty.call(cur.children,key)){
35+
constchild=cur.children[key];
36+
37+
if(dfs(i+1,child)){
38+
returntrue;
39+
}
40+
}
41+
}
42+
returnfalse;
43+
}else{
44+
if(!(cincur.children)){
45+
returnfalse;
46+
}
47+
cur=cur.children[c];
48+
}
49+
}
50+
51+
returncur.endOfWord;
52+
}
53+
returndfs(0,this.root);
54+
}
55+
}

‎javascript/22-Generate-Parentheses.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
functiongenerateParenthesis(n){
2+
conststack=[];
3+
constres=[];
4+
5+
functionbacktrack(openN,closedN){
6+
if(openN===n&&closedN===n){
7+
res.push(stack.join(""));
8+
return;
9+
}
10+
11+
if(openN<n){
12+
backtrack(openN+1,closedN);
13+
stack.pop();
14+
stack.push("(");
15+
}
16+
17+
if(closedN<openN){
18+
backtrack(openN,closedN+1);
19+
stack.push(")");
20+
stack.pop();
21+
}
22+
}
23+
24+
backtrack(0,0);
25+
26+
returnres;
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
functionkthSmallest(root,k){
2+
letn=0;
3+
conststack=[];
4+
letcur=root;
5+
6+
while(cur||stack){
7+
while(cur){
8+
stack.push(cur);
9+
cur=cur.left;
10+
}
11+
12+
cur=stack.pop();
13+
n+=1;
14+
15+
if(n==k){
16+
returncur.val;
17+
}
18+
19+
cur=cur?.right;
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
functionfindDuplicate(nums){
2+
letslow=0;
3+
letfast=0;
4+
5+
while(true){
6+
slow=nums[slow];
7+
fast=nums[nums[fast]];
8+
if(slow==fast){
9+
break;
10+
}
11+
}
12+
13+
letslow2=0;
14+
15+
while(true){
16+
slow=nums[slow];
17+
slow2=nums[slow2];
18+
if(slow==slow2){
19+
break;
20+
}
21+
}
22+
23+
returnslow;
24+
}

‎javascript/46-Permutations.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
functionpermute(nums){
2+
constres=[];
3+
4+
if(nums.length===1){
5+
return[nums.slice()];
6+
}
7+
8+
for(constiofnums){
9+
letn=nums.shift();
10+
11+
letperms=permute(nums);
12+
13+
for(constpermofperms){
14+
perm.push(n);
15+
}
16+
17+
perms.forEach((perm)=>{
18+
res.push(perm);
19+
});
20+
21+
nums.push(n);
22+
}
23+
24+
returnres;
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp