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

Commitc387a9c

Browse files
committed
add shortest distance
1 parent2e9a88c commitc387a9c

File tree

7 files changed

+139
-8
lines changed

7 files changed

+139
-8
lines changed

‎Easy/100-sameTree.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
*@param {TreeNode} q
1111
*@return {boolean}
1212
*/
13-
varisSameTree=function(p,q){
14-
if(!p&&!q)returntrue;
15-
if(p&&q&&p.val===q.val){
16-
if(isSameTree(p.left,q.left)&&isSameTree(p.right,q.right))returntrue;
17-
}
18-
returnfalse;
19-
};
13+
varisSameTree=function(p,q){
14+
if(!p||!q){
15+
returnp===q;
16+
}
17+
18+
if(p.val===q.val){
19+
returnisSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
20+
}
21+
22+
returnfalse;
23+
};

‎Easy/101-symmetricTree.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ var helper = function(left, right) {
3333
returnfalse;
3434
}
3535

36+
// third try
37+
varhelper=function(left,right){
38+
if(!left&&!right){
39+
returntrue;
40+
}elseif(!left||!right){
41+
returnfalse;
42+
}else{
43+
if(left.val===right.val){
44+
returnisSymmetricHelper(left.left,right.right)&&isSymmetricHelper(left.right,right.left);
45+
}else{
46+
returnfalse;
47+
}
48+
}
49+
}
50+
3651
// iterative
3752
varisSymmetric=function(root){
3853
if(!root)returntrue;

‎Easy/160-IntersectionofTwoLinkedLists.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var getIntersectionNode = function(headA, headB) {
9494
pa=pa.next;
9595
pb=pb.next;
9696
if(pa===pb){
97-
returnpa;
97+
returnpa;
9898
}
9999

100100
if(!pa){

‎Easy/243-shortestWordDist.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* question: http://www.programcreek.com/2014/08/leetcode-shortest-word-distance-java/
3+
*
4+
*/
5+
6+
functionshortestWordDist(words,word1,word2){
7+
varindex1,index2;
8+
vardist=Math.pow(2,32)-1;// for some reason in JavaScript, manually set max.
9+
10+
words.forEach(function(word,index){
11+
if(word===word1){
12+
index1=index;
13+
}
14+
15+
if(word===word2){
16+
index2=index;
17+
}
18+
19+
if(index1>=0&&index2>=0){
20+
dist=Math.min(Math.abs(index1-index2),dist);
21+
}
22+
})
23+
24+
returndist;
25+
}
26+
27+
// test cases
28+
varwords=["practice","makes","perfect","coding","makes"];
29+
varword1='practice';
30+
varword2='coding';
31+
varword3='makes';
32+
console.log(shortestWordDist(words,word1,word2));
33+
console.log(shortestWordDist(words,word2,word3));

‎Medium/236-lcaBinaryTree.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
*@param {TreeNode} root
10+
*@param {TreeNode} p
11+
*@param {TreeNode} q
12+
*@return {TreeNode}
13+
*/
14+
varlowestCommonAncestor=function(root,p,q){
15+
if(!root||root===p||root===q){
16+
returnroot;
17+
}
18+
19+
varleft=lowestCommonAncestor(root.left,p,q);
20+
varright=lowestCommonAncestor(root.right,p,q);
21+
22+
if(!left){
23+
returnright;
24+
}
25+
if(!right){
26+
returnleft;
27+
}
28+
29+
returnroot;
30+
};

‎Medium/244-shortestWordDistII.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* question: http://www.programcreek.com/2014/07/leetcode-shortest-word-distance-ii-java/
3+
*
4+
*/
5+
6+
functionShortestWordDist(words){
7+
this.hashMap={};
8+
9+
varmap=this.hashMap;
10+
11+
words.forEach(function(word,index){
12+
if(wordinmap){
13+
map[word].push(index);
14+
}else{
15+
map[word]=[index];
16+
}
17+
});
18+
}
19+
20+
ShortestWordDist.prototype.shortest=function(word1,word2){
21+
varlist1=this.hashMap[word1];
22+
varlist2=this.hashMap[word2];
23+
vardist=Math.pow(2,32)-1;
24+
25+
for(vari=0,j=0;i<list1.length&&j<list2.length;){
26+
varindex1=list1[i];
27+
varindex2=list2[j];
28+
29+
dist=Math.min(Math.abs(index1-index2),dist);
30+
if(index1<index2){
31+
i++;
32+
}else{
33+
j++;
34+
}
35+
}
36+
37+
returndist;
38+
};
39+
40+
// test cases
41+
varws=["practice","makes","perfect","coding","makes","google","coding","apple","alpha"];
42+
varwords=newShortestWordDist(ws);
43+
44+
varword1='practice';
45+
varword2='coding';
46+
varword3='apple';
47+
console.log(words.shortest(word1,word2));
48+
console.log(words.shortest(word2,word3));

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
*[221. Maximal Square](https://leetcode.com/problems/maximal-square/) -[Solution](./Medium/221-maximalSquare.js)
166166
*[229. Majority Element II](https://leetcode.com/problems/majority-element-ii/) -[Solution](./Medium/229-majorityElementII.js)
167167
*[230. Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) -[Solution](./Medium/230-kthSmallestElementinBST.js)
168+
*[236. Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) -[Solution](./Medium/236-lcaBinaryTree.js)
168169
*[238. Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) -[Solution](./Medium/238-productExceptSelf.js)
169170
*[240. Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) -[Solution](./Medium/240-Search2DMatrixII.js)
170171
*[241. Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) -[Solution](./Medium/241-differentWaysAddParentheses.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp