Skip to content

Commit dfbaf07

Browse files
committed
2 parents 0816077 + b44b956 commit dfbaf07

9 files changed

+277
-1
lines changed

Easy/344-reverseString.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var reverseString = function(s) {
6+
return s.split('').reverse().join('');
7+
};

Easy/349-intersectionTwoArrays.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number[]}
5+
*/
6+
// use hashset idea, O(N)
7+
var intersection = function(nums1, nums2) {
8+
// Note ES5 doesn't have set
9+
var set1 = new Set();
10+
var interSet = new Set();
11+
for (var i = 0; i < nums1.length; i++) {
12+
set1.add(nums1[i]);
13+
}
14+
15+
for (var j = 0; j < nums2.length; j++) {
16+
if (set1.has(nums2[j])) {
17+
interSet.add(nums2[j]);
18+
}
19+
}
20+
21+
var result = [];
22+
interSet.forEach(function(item) {
23+
result.push(item);
24+
});
25+
26+
return result;
27+
};
28+
29+
// solution 2: sort first. O(nlogn)
30+
var intersection = function(nums1, nums2) {
31+
// Note ES5 doesn't have set
32+
var interSet = new Set();
33+
nums1.sort(comparator);
34+
nums2.sort(comparator);
35+
var i = 0;
36+
var j = 0;
37+
38+
while (i < nums1.length) {
39+
if (j === nums2.length) break;
40+
if (nums1[i] < nums2[j]) {
41+
i++;
42+
} else if (nums1[i] > nums2[j]) {
43+
j++;
44+
} else {
45+
interSet.add(nums1[i]);
46+
i++;
47+
j++;
48+
}
49+
}
50+
51+
var result = [];
52+
interSet.forEach(function(item) {
53+
result.push(item);
54+
});
55+
56+
return result;
57+
};
58+
59+
var comparator = function(a, b) {
60+
return a - b;
61+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number[]}
5+
*/
6+
var intersect = function(nums1, nums2) {
7+
var hashMap = {};
8+
for (var i = 0; i < nums1.length; i++) {
9+
if (hashMap.hasOwnProperty(nums1[i])) {
10+
hashMap[nums1[i]]++;
11+
} else {
12+
hashMap[nums1[i]] = 1;
13+
}
14+
}
15+
var hashMap2 = {};
16+
for (i = 0; i < nums2.length; i++) {
17+
if (hashMap2.hasOwnProperty(nums2[i])) {
18+
hashMap2[nums2[i]]++;
19+
} else {
20+
hashMap2[nums2[i]] = 1;
21+
}
22+
}
23+
24+
var result = [];
25+
for (var key in hashMap) {
26+
if (!hashMap2.hasOwnProperty(key)) continue;
27+
var numAppears = Math.min(hashMap[key], hashMap2[key]);
28+
for (i = 0; i < numAppears; i++) {
29+
result.push(parseInt(key));
30+
}
31+
}
32+
33+
return result;
34+
};
35+
36+
// A better and more concise solution. One hashmap needed.
37+
var intersect = function(nums1, nums2) {
38+
var hashMap = {};
39+
for (var i = 0; i < nums1.length; i++) {
40+
if (hashMap.hasOwnProperty(nums1[i])) {
41+
hashMap[nums1[i]]++;
42+
} else {
43+
hashMap[nums1[i]] = 1;
44+
}
45+
}
46+
47+
var result = [];
48+
for (i = 0; i < nums2.length; i++) {
49+
if (hashMap.hasOwnProperty(nums2[i]) && hashMap[nums2[i]] > 0) {
50+
result.push(nums2[i]);
51+
hashMap[nums2[i]]--;
52+
}
53+
}
54+
55+
return result;
56+
};

Medium/129-sumRootToLeafNumbers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,19 @@ var helper = function(root, sum) {
4747
return helper(root.left, 10 * sum + root.val)
4848
+ helper(root.right, 10 * sum + root.val);
4949
};
50+
51+
// a third DFS method. Top-down
52+
var sumNumbers = function(root) {
53+
if (!root) return 0;
54+
return helper(root, 0, 0);
55+
};
56+
57+
function helper(root, currNum, sum) {
58+
if (!root) return sum;
59+
currNum = root.val + 10 * currNum;
60+
if (!root.left && !root.right) {
61+
sum += currNum;
62+
return sum;
63+
}
64+
return helper(root.left, currNum, sum) + helper(root.right, currNum, sum)
65+
}

Medium/147-insertionSortList.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var insertionSortList = function(head) {
13+
if (!head) return head;
14+
var dummyHead = new ListNode(null);
15+
dummyHead.next = head;
16+
var first = dummyHead;
17+
var second = head;
18+
19+
while (second.next) {
20+
if (second.next.val < second.val) {
21+
var smallNode = second.next;
22+
second.next = second.next.next;
23+
while (first.next && first.next.val < smallNode.val) {
24+
first = first.next;
25+
}
26+
smallNode.next = first.next;
27+
first.next = smallNode;
28+
first = dummyHead;
29+
} else {
30+
second = second.next;
31+
if (!second) break;
32+
}
33+
34+
}
35+
36+
return dummyHead.next;
37+
};

Medium/148-sortList.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* sort, then merge. In fact, this method use O(logN) space for recursive function calls.
10+
* needs to be improved.
11+
*
12+
* @param {ListNode} head
13+
* @return {ListNode}
14+
*/
15+
var sortList = function(head) {
16+
if (!head || !head.next) return head;
17+
var fast = head;
18+
var slow = head;
19+
20+
while (fast.next && fast.next.next) {
21+
slow = slow.next;
22+
fast = fast.next.next;
23+
}
24+
fast = slow.next;
25+
slow.next = null;
26+
var secondHalf = sortList(fast);
27+
var firstHalf = sortList(head);
28+
return merge(firstHalf, secondHalf);
29+
};
30+
31+
var merge = function(l1, l2) {
32+
var l3 = new ListNode();
33+
var l3Head = l3;
34+
while (l1 && l2) {
35+
if (l1.val <= l2.val ) {
36+
l3.next = l1;
37+
l1 = l1.next;
38+
} else {
39+
l3.next = l2;
40+
l2 = l2.next;
41+
}
42+
l3 = l3.next;
43+
}
44+
if (!l1) l3.next = l2;
45+
if (!l2) l3.next = l1;
46+
47+
return l3Head.next;
48+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string[]} tokens
3+
* @return {number}
4+
*/
5+
// seems like it is not a good solution
6+
var evalRPN = function(tokens) {
7+
var stack = [];
8+
for (var i = 0; i < tokens.length; i++) {
9+
var token = tokens[i];
10+
if (!isNaN(token)) {
11+
stack.push(token);
12+
} else {
13+
var num1 = stack.pop();
14+
var num2 = stack.pop();
15+
var result = getResult(num2, num1, token);
16+
stack.push(result);
17+
}
18+
}
19+
20+
return Math.floor(parseInt(stack[stack.length - 1]));
21+
};
22+
23+
var getResult = function(num1, num2, operator) {
24+
var a = parseInt(num1);
25+
var b = parseInt(num2);
26+
if (operator === '+') return a + b;
27+
if (operator === '-') return a - b;
28+
if (operator === '/') return a / b;
29+
if (operator === '*') return a * b;
30+
};

Medium/338-countingBits.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Solution: f(i) = f(i/2) + i % 2;
3+
* the bit nums = the bit nums of half of the number, and plus 1 if the last digit of the bit number is 1.
4+
*
5+
* @param {number} num
6+
* @return {number[]}
7+
*/
8+
var countBits = function(num) {
9+
var result = [0];
10+
for (var i = 1; i <= num; i++) {
11+
result[i] = result[i >> 1] + (i & 1);
12+
}
13+
14+
return result;
15+
};

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@
6868
* [326. Power of Three](https://leetcode.com/problems/power-of-three/) - [Solution](./Easy/326-powerOfThree.js)
6969
* [328. Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) - [Solution](./Easy/328-oddevenLinkedList.js)
7070
* [342. Power of Four](https://leetcode.com/problems/power-of-four/) - [Solution](./Easy/342-powerOfFour.js)
71+
* [344. Reverse String](https://leetcode.com/problems/reverse-string/) - [Solution](./Easy/344-reverseString.js)
72+
* [349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) - [Solution](./Easy/349-intersectionTwoArrays.js)
73+
* [350. Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) - [Solution](./Easy/349-intersectionTwoArraysII.js)
7174
* [412. Fizz Buzz](https://leetcode.com/problems/fizz-buzz/?tab=Solutions) - [Solution](./Easy/412-fizzBuzz.js)
7275

73-
7476
##### Medium
7577
* [1. Two Sum](https://leetcode.com/problems/two-sum/) - [Solution](./Medium/1-twoSum.js)
7678
* [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) - [Solution](./Medium/2-addTwoNumbers.js)
@@ -145,6 +147,9 @@
145147
* [142. Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) - [Solution](./Medium/142-linkedListCycleII.js)
146148
* [143. Reorder List](https://leetcode.com/problems/reorder-list/) - [Solution](./Medium/143-reorderList.js)
147149
* [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) - [Solution](./Medium/144-binaryTreePreorder.js)
150+
* [147. Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/) - [Solution](./Medium/147-insertionSortList.js)
151+
* [148. Sort List](https://leetcode.com/problems/sort-list/) - [Solution](./Medium/148-sortList.js)
152+
* [150. Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) - [Solution](./Medium/150-reversePolishNotation.js)
148153
* [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) - [Solution](./Medium/151-reverseWordsInAString.js)
149154
* [152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) - [Solution](./Medium/152-MaxProductSubarray.js)
150155
* [153. Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) - [Solution](./Medium/153-findMinimumInRotatedSortedArray.js)
@@ -171,6 +176,7 @@
171176
* [319. Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) - [Solution](./Medium/319-bulbSwitcher.js)
172177
* [331. Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) - [Solution](./Medium/331-verifyBinaryTree.js)
173178
* [337. House Robber III](https://leetcode.com/problems/house-robber-iii/) - [Solution](./Medium/337-houseRobberIII.js)
179+
* [338. Counting Bits](https://leetcode.com/problems/counting-bits/) - [Solution](./Medium/338-countingBits.js)
174180
* [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) - [Solution](./Medium/247-topKFrequentElements.js)
175181

176182
##### Hard

0 commit comments

Comments
 (0)