Skip to content

Commit c387a9c

Browse files
committed
add shortest distance
1 parent 2e9a88c commit c387a9c

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-
var isSameTree = function(p, q) {
14-
if (!p && !q) return true;
15-
if (p && q && p.val === q.val) {
16-
if (isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) return true;
17-
}
18-
return false;
19-
};
13+
var isSameTree = function(p, q) {
14+
if (!p || !q) {
15+
return p === q;
16+
}
17+
18+
if (p.val === q.val) {
19+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
20+
}
21+
22+
return false;
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
return false;
3434
}
3535

36+
// third try
37+
var helper = function(left, right) {
38+
if (!left && !right) {
39+
return true;
40+
} else if (!left || !right) {
41+
return false;
42+
} else {
43+
if (left.val === right.val) {
44+
return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
45+
} else {
46+
return false;
47+
}
48+
}
49+
}
50+
3651
// iterative
3752
var isSymmetric = function(root) {
3853
if (!root) return true;

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-
return pa ;
97+
return pa;
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+
function shortestWordDist(words, word1, word2) {
7+
var index1, index2;
8+
var dist = 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+
return dist;
25+
}
26+
27+
// test cases
28+
var words = ["practice", "makes", "perfect", "coding", "makes"];
29+
var word1 = 'practice';
30+
var word2 = 'coding';
31+
var word3 = '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+
var lowestCommonAncestor = function(root, p, q) {
15+
if (!root || root === p || root === q) {
16+
return root;
17+
}
18+
19+
var left = lowestCommonAncestor(root.left, p, q);
20+
var right = lowestCommonAncestor(root.right, p, q);
21+
22+
if (!left) {
23+
return right;
24+
}
25+
if (!right) {
26+
return left;
27+
}
28+
29+
return root;
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+
function ShortestWordDist(words) {
7+
this.hashMap = {};
8+
9+
var map = this.hashMap;
10+
11+
words.forEach(function(word, index) {
12+
if (word in map) {
13+
map[word].push(index);
14+
} else {
15+
map[word] = [index];
16+
}
17+
});
18+
}
19+
20+
ShortestWordDist.prototype.shortest = function(word1, word2) {
21+
var list1 = this.hashMap[word1];
22+
var list2 = this.hashMap[word2];
23+
var dist = Math.pow(2, 32) - 1;
24+
25+
for (var i = 0, j = 0; i < list1.length && j < list2.length;) {
26+
var index1 = list1[i];
27+
var index2 = 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+
return dist;
38+
};
39+
40+
// test cases
41+
var ws = ["practice", "makes", "perfect", "coding", "makes", "google", "coding", "apple", "alpha"];
42+
var words = new ShortestWordDist(ws);
43+
44+
var word1 = 'practice';
45+
var word2 = 'coding';
46+
var word3 = '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)