Skip to content

Commit 985bb9a

Browse files
committed
leetcode
1 parent 34171a1 commit 985bb9a

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
3+
4+
-* 350. Intersection of Two Arrays II *-
5+
6+
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
7+
8+
9+
10+
Example 1:
11+
12+
Input: nums1 = [1,2,2,1], nums2 = [2,2]
13+
Output: [2,2]
14+
Example 2:
15+
16+
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
17+
Output: [4,9]
18+
Explanation: [9,4] is also accepted.
19+
20+
21+
Constraints:
22+
23+
1 <= nums1.length, nums2.length <= 1000
24+
0 <= nums1[i], nums2[i] <= 1000
25+
26+
27+
Follow up:
28+
29+
What if the given array is already sorted? How would you optimize your algorithm?
30+
What if nums1's size is small compared to nums2's size? Which algorithm is better?
31+
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?
32+
33+
*/
34+
35+
import 'dart:collection';
36+
37+
class A {
38+
List<int> intersect(List<int> nums1, List<int> nums2) {
39+
if (nums1.isEmpty ||
40+
nums2.isEmpty ||
41+
nums1.length == 0 ||
42+
nums2.length == 0) return [];
43+
HashMap<int, int> map = HashMap();
44+
List<int> result = [];
45+
for (int number in nums1)
46+
if (map.containsKey(number))
47+
map[number] = map[number]! + 1;
48+
else
49+
map[number] = 1;
50+
for (int number in nums2) {
51+
if (map.containsKey(number) && map[number]! > 0) {
52+
result.add(number);
53+
int freq = map[number]!;
54+
freq--;
55+
map[number] = freq;
56+
}
57+
}
58+
return listToArray(result);
59+
}
60+
61+
List<int> listToArray(List<int> list) {
62+
List<int> result = List.filled(list.length, 0);
63+
for (int i = 0; i < list.length; i++) {
64+
result[i] = list.elementAt(i);
65+
}
66+
return result;
67+
}
68+
}
69+
70+
class B {
71+
List<int> intersect(List<int> nums1, List<int> nums2) {
72+
nums1.sort();
73+
nums2.sort();
74+
List<int> res = [];
75+
int left = 0, right = 0;
76+
while (left < nums1.length && right < nums2.length) {
77+
if (nums1[left] == nums2[right]) {
78+
res.add(nums1[left]);
79+
left++;
80+
right++;
81+
} else if (nums1[left] < nums2[right]) {
82+
left++;
83+
} else
84+
right++;
85+
}
86+
return res;
87+
}
88+
}
89+
90+
class C {
91+
List<int> intersect(List<int> nums1, List<int> nums2) {
92+
List<int> arr = List.filled(1001, 0);
93+
List<int> ans = List.filled(1001, 0);
94+
int count = 0;
95+
for (int i in nums1) {
96+
arr[i]++;
97+
}
98+
for (int i in nums2) {
99+
if (arr[i] > 0) {
100+
ans[count++] = i;
101+
arr[i]--;
102+
}
103+
}
104+
105+
// return List.copyOfRange(ans,0,count);
106+
return List.copyRange(ans, 0, [0, count]) as List<int>;
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 🔥 Intersection of Two Arrays II 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class Solution {
9+
List<int> intersect(List<int> nums1, List<int> nums2) {
10+
if (nums1.isEmpty ||
11+
nums2.isEmpty ||
12+
nums1.length == 0 ||
13+
nums2.length == 0) return [];
14+
HashMap<int, int> map = HashMap();
15+
List<int> result = [];
16+
for (int number in nums1)
17+
if (map.containsKey(number))
18+
map[number] = map[number]! + 1;
19+
else
20+
map[number] = 1;
21+
for (int number in nums2) {
22+
if (map.containsKey(number) && map[number]! > 0) {
23+
result.add(number);
24+
int freq = map[number]!;
25+
freq--;
26+
map[number] = freq;
27+
}
28+
}
29+
return listToArray(result);
30+
}
31+
32+
List<int> listToArray(List<int> list) {
33+
List<int> result = List.filled(list.length, 0);
34+
for (int i = 0; i < list.length; i++) {
35+
result[i] = list.elementAt(i);
36+
}
37+
return result;
38+
}
39+
}
40+
```
41+
42+
## Solution - 2
43+
44+
```dart
45+
class Solution {
46+
List<int> intersect(List<int> nums1, List<int> nums2) {
47+
nums1.sort();
48+
nums2.sort();
49+
List<int> res = [];
50+
int left = 0, right = 0;
51+
while (left < nums1.length && right < nums2.length) {
52+
if (nums1[left] == nums2[right]) {
53+
res.add(nums1[left]);
54+
left++;
55+
right++;
56+
} else if (nums1[left] < nums2[right]) {
57+
left++;
58+
} else
59+
right++;
60+
}
61+
return res;
62+
}
63+
}
64+
```
65+
66+
## Solution - 3
67+
68+
```dart
69+
class Solution {
70+
List<int> intersect(List<int> nums1, List<int> nums2) {
71+
List<int> arr = List.filled(1001, 0);
72+
List<int> ans = List.filled(1001, 0);
73+
int count = 0;
74+
for (int i in nums1) {
75+
arr[i]++;
76+
}
77+
for (int i in nums2) {
78+
if (arr[i] > 0) {
79+
ans[count++] = i;
80+
arr[i]--;
81+
}
82+
}
83+
return List.copyRange(ans, 0, [0, count]) as List<int>;
84+
}
85+
}
86+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
134134
- [**344.** Reverse String](ReverseString/reverse_string.dart)
135135
- [**349.** Intersection of Two Arrays](IntersectionOfTwoArrays/intersection_of_two_arrays.dart)
136136
- [**223.** Rectangle Area](RectangleArea/rectangle_area.dart)
137+
- [**350.** Intersection of Two Arrays II](IntersectionOfTwoArraysII/intersection_of_two_arrays_II.dart)
137138

138139
## Reach me via
139140

0 commit comments

Comments
 (0)