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

Commit1409b67

Browse files
committed
update solution
1 parent36b4334 commit1409b67

File tree

7 files changed

+142
-7
lines changed

7 files changed

+142
-7
lines changed

‎Easy/243-shortestWordDist.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
functionshortestWordDist(words,word1,word2){
77
varindex1,index2;
8-
vardist=Math.pow(2,32)-1;// for some reason in JavaScript, manually set max.
98

10-
words.forEach(function(word,index){
9+
returnwords.reduce(function(dist,word,index){
1110
if(word===word1){
1211
index1=index;
1312
}
@@ -19,9 +18,9 @@ function shortestWordDist(words, word1, word2) {
1918
if(index1>=0&&index2>=0){
2019
dist=Math.min(Math.abs(index1-index2),dist);
2120
}
22-
})
2321

24-
returndist;
22+
returndist;
23+
},Number.MAX_VALUE);
2524
}
2625

2726
// test cases

‎Easy/367-perfectSquare.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*@param {number} num
3+
*@return {boolean}
4+
*/
5+
varisPerfectSquare=function(num){
6+
varlo=1;
7+
varhi=num;
8+
varisPS=false;
9+
10+
if(num===1){
11+
isPS=true;
12+
}
13+
14+
while(lo<=hi){
15+
varmid=lo+Math.floor((hi-lo)/2);
16+
varmidSquare=mid*mid;
17+
if(midSquare===num){
18+
isPS=true;
19+
break;
20+
}elseif(midSquare>num){
21+
hi=mid-1;
22+
}else{
23+
lo=mid+1;
24+
}
25+
}
26+
27+
returnisPS;
28+
};

‎Hard/273-integerToEnglish.js‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*@return {string}
44
*/
55

6-
// my own solution
6+
// my own solution
77
varnumberToWords=function(num){
88
vardict={1:'One',2:'Two',3:'Three',4:'Four',5:'Five',
99
6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',
@@ -86,3 +86,44 @@ var twoDigits = function twoDigits(num, dict) {
8686

8787
returnstr;
8888
}
89+
90+
91+
// a more concise way
92+
varzeroToTwenty=['','One','Two','Three','Four','Five',
93+
'Six','Seven','Eight','Nine','Ten','Eleven','Twelve',
94+
'Thirteen','Fourteen','Fifteen','Sixteen','Seventeen',
95+
'Eighteen','Nineteen','Twenty'];
96+
vartwentyToNinety=['','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'];
97+
varthousand=['','Thousand','Million','Billion'];
98+
99+
varnumberToWords=function(num){
100+
varstr='';
101+
102+
if(num===0){
103+
return'Zero';
104+
}
105+
106+
for(vari=0;num>0;i++){
107+
varh=num%1000;
108+
if(h>0){// in case 1,000,000
109+
str=helper(h)+thousand[i]+' '+str;
110+
}
111+
num=Math.floor(num/1000);
112+
}
113+
114+
returnstr.trim();
115+
};
116+
117+
functionhelper(num){
118+
if(num===0){
119+
return'';
120+
}elseif(num<=20){
121+
returnzeroToTwenty[num]+' ';
122+
}elseif(num<100){
123+
varh=Math.floor(num/10);
124+
returntwentyToNinety[h-1]+' '+helper(num%10);
125+
}else{
126+
varh=Math.floor(num/100);
127+
returnzeroToTwenty[h]+' Hundred '+helper(num%100);
128+
}
129+
}

‎Medium/244-shortestWordDistII.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function ShortestWordDist(words) {
2020
ShortestWordDist.prototype.shortest=function(word1,word2){
2121
varlist1=this.hashMap[word1];
2222
varlist2=this.hashMap[word2];
23-
vardist=Math.pow(2,32)-1;
23+
vardist=Number.MAX_VALUE;
2424

2525
for(vari=0,j=0;i<list1.length&&j<list2.length;){
2626
varindex1=list1[i];

‎Medium/245-shortestWordDistIII.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
functionshortestWordDist(words,word1,word2){
77
varindex1=-1;
88
varindex2=-1;
9-
vardist=Math.pow(2,32)-1;// for some reason in JavaScript, manually set max.
9+
vardist=Number.MAX_VALUE;// for some reason in JavaScript, manually set max.
1010

1111
words.forEach(function(word,index){
1212
if(word1===word2){

‎Medium/515-largestValEachTree.js‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
// my own solution BFS, iterative
10+
/**
11+
*@param {TreeNode} root
12+
*@return {number[]}
13+
*/
14+
varlargestValues=function(root){
15+
varnext=[root];
16+
varmaxArr=[];
17+
18+
if(!root){
19+
returnmaxArr;
20+
}
21+
22+
while(next.length>0){
23+
varcurr=next.slice();
24+
varmax=-Number.MAX_VALUE;
25+
next=[];
26+
27+
while(curr.length>0){
28+
varnode=curr.shift();
29+
max=Math.max(node.val,max);
30+
31+
if(node.left){
32+
next.push(node.left);
33+
}
34+
if(node.right){
35+
next.push(node.right);
36+
}
37+
}
38+
39+
maxArr.push(max);
40+
}
41+
42+
returnmaxArr;
43+
};
44+
45+
// DFS
46+
varlargestValues=function(root){
47+
varresult=[];
48+
helper(root,result,0);
49+
returnresult;
50+
};
51+
52+
varhelper=function(root,res,level){
53+
if(!root){
54+
return;
55+
}
56+
57+
if(res.length===level){
58+
res.push(root.val);
59+
}else{
60+
res[level]=Math.max(res[level],root.val);
61+
}
62+
63+
helper(root.left,res,level+1);
64+
helper(root.right,res,level+1);
65+
}

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
*[344. Reverse String](https://leetcode.com/problems/reverse-string/) -[Solution](./Easy/344-reverseString.js)
7676
*[349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) -[Solution](./Easy/349-intersectionTwoArrays.js)
7777
*[350. Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) -[Solution](./Easy/349-intersectionTwoArraysII.js)
78+
*[367. Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) -[Solution](./Easy/367-perfectSquare.js)
7879
*[412. Fizz Buzz](https://leetcode.com/problems/fizz-buzz/?tab=Solutions) -[Solution](./Easy/412-fizzBuzz.js)
7980

8081
#####Medium
@@ -185,6 +186,7 @@
185186
*[337. House Robber III](https://leetcode.com/problems/house-robber-iii/) -[Solution](./Medium/337-houseRobberIII.js)
186187
*[338. Counting Bits](https://leetcode.com/problems/counting-bits/) -[Solution](./Medium/338-countingBits.js)
187188
*[347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) -[Solution](./Medium/247-topKFrequentElements.js)
189+
*[515. Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) -[Solution](./Medium/515-largestValEachTree.js)
188190

189191
#####Hard
190192
*[23. Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) -[Solution](./Hard/23-MergeKSortedLists.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp