Skip to content

Commit 3a17c48

Browse files
committed
add js solution for leetcode 349 and 350
1 parent 68a665d commit 3a17c48

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ LeetCode
129129
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/description/) | |Easy|
130130
|367|[Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/description/) | |Easy|
131131
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/) | |Medium|
132-
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | |Easy|
133-
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | |Easy|
132+
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [js](./algorithms/intersectionOfTwoArraysII/intersectionOfTwoArraysII.js) |Easy|
133+
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [js](./algorithms/intersectionOfTwoArrays/intersectionOfTwoArrays.js) |Easy|
134134
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | |Medium|
135135
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | |Easy|
136136
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) [java](./algorithms/resverseString/Solution.java) | |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var intersection = function(nums1, nums2) {
2+
const set = new Set();
3+
nums1.forEach(x => set.add(x));
4+
5+
const res = [];
6+
nums2.forEach(y => {
7+
if (set.has(y)) {
8+
res.push(y);
9+
set.delete(y);
10+
}
11+
})
12+
return res;
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var intersect = function(nums1, nums2) {
2+
const map = new Map();
3+
nums1.forEach(x => {
4+
if (map.has(x)) {
5+
map.set(x, map.get(x) + 1);
6+
} else {
7+
map.set(x, 1);
8+
}
9+
});
10+
const res = [];
11+
nums2.forEach(y => {
12+
if (map.has(y)) {
13+
res.push(y);
14+
map.set(y, map.get(y) - 1);
15+
if(map.get(y) === 0) {
16+
map.delete(y);
17+
}
18+
}
19+
});
20+
return res;
21+
};

0 commit comments

Comments
 (0)