leetcode 350. Intersection of Two Arrays II
題目:
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9]
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to nums2's size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
第一個想到的辦法就是存hashmap,計算出每個數字在每個陣列裡出現的次數,然後遍歷一遍hashmap把兩邊同時出現的數字取出現次數最小的次數加入result就可以了。時間複雜度O(max(m ,n))吧。看了解答其實不需要兩個map,直接第一個map存完後在遍歷nums2的時候查它在不在map裡就行了,程式碼懶得寫了orz
Runtime: 3 ms, faster than 43.83% of Java online submissions for Intersection of Two Arrays II.
Memory Usage: 39.6 MB, less than 15.22% of Java online submissions for Intersection of Two Arrays II.
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> result = new ArrayList<>();
Map<Integer, Integer> map1 = new HashMap<>();
Map<Integer, Integer> map2 = new HashMap<>();
for (int i : nums1) {
map1.put(i, map1.getOrDefault(i, 0) + 1);
}
for (int i : nums2) {
map2.put(i, map2.getOrDefault(i, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
int key = entry.getKey();
if (map2.containsKey(key)) {
int value = Math.min(entry.getValue(), map2.get(key));
for (int i = 0; i < value; i++) {
result.add(key);
}
}
}
int[] resultArray = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
resultArray[i] = result.get(i);
}
return resultArray;
}
}
也可以先sort完了以後用two pointers O(n)一次解決。剛開始不知道為啥可能受了剛剛做duplicate的影響所以以為要整duplicate就寫的很麻煩(被註釋掉的部分),後來看了解答才發現想多了orz。
Runtime: 2 ms, faster than 96.63% of Java online submissions for Intersection of Two Arrays II.
Memory Usage: 39.2 MB, less than 15.22% of Java online submissions for Intersection of Two Arrays II.
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int p1 = 0;
int p2 = 0;
List<Integer> result = new ArrayList<>();
while (p1 != nums1.length && p2 != nums2.length) {
if (nums1[p1] < nums2[p2]) {
p1++;
} else if (nums1[p1] > nums2[p2]) {
p2++;
} else {
/*int count1 = 1;
while (p1 + 1 != nums1.length && nums1[p1] == nums1[p1 + 1]) {
p1++;
count1++;
}
int count2 = 1;
while (p2 + 1 != nums2.length && nums2[p2] == nums2[p2 + 1]) {
p2++;
count2++;
}
int count = Math.min(count1, count2);
for (int i = 0; i < count; i++) {
result.add(nums1[p1]);
}*/
result.add(nums1[p1]);
p1++;
p2++;
}
}
int[] resultArray = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
resultArray[i] = result.get(i);
}
return resultArray;
}
}
相關文章
- LeetCode | 349 Intersection Of Two ArraysLeetCode
- Leetcode 160. Intersection of Two Linked ListsLeetCode
- Leetcode 4 Median of Two Sorted ArraysLeetCode
- LeetCode 4. Median of Two Sorted ArraysLeetCode
- LeetCode Median of Two Sorted Arrays(004)解法總結LeetCode
- [LeetCode] 1385. Find the Distance Value Between Two ArraysLeetCode
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- CodeForces 1417B Two Arrays
- Leetcode 231 Power of TwoLeetCode
- Leetcode 1 two sumLeetCode
- LeetCode | 1 Two SumLeetCode
- leetcode 4. Median of Two Sorted Arrays 尋找兩個正序陣列的中位數(困難)LeetCode陣列
- Leetcode 29 Divide Two IntegersLeetCodeIDE
- LeetCode 2 Add Two NumbersLeetCode
- LeetCode-1 Two SumLeetCode
- [LeetCode]1.Two SumLeetCode
- Leetcode 231. Power of TwoLeetCode
- python: leetcode - 1 Two SumPythonLeetCode
- leetcode-29. Divide Two IntegersLeetCodeIDE
- LeetCode-2 Add Two NumbersLeetCode
- Leetcode 21 Merge Two Sorted ListsLeetCode
- LeetCode: Two sum(兩數之和)LeetCode
- [LeetCode] 29. Divide Two IntegersLeetCodeIDE
- LeetCode 2. Add Two NumbersLeetCode
- 【Leetcode】1029. Two City SchedulingLeetCode
- leetcode 371. Sum of Two IntegersLeetCode
- leetcode-90. Subsets IILeetCode
- Leetcode 40 Combination Sum IILeetCode
- Leetcode 213 House Robber IILeetCode
- LeetCode 1103[分糖果II]LeetCode
- CF 773 (Div. 1) D. Two Arrays 雙指標 容斥指標
- LeetCode 21. Merge Two Sorted ListsLeetCode
- LeetCode #1:Two Sum(簡單題)LeetCode
- LeetCode Two Sum(001)解法總結LeetCode
- 【LeetCode】617. Merge Two Binary TreesLeetCode
- LeetCode 617. Merge Two Binary TreesLeetCode
- LeetCode T29 Divide Two IntegersLeetCodeIDE
- [LeetCode] 210. Course Schedule IILeetCode