Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0532cec

Browse files
committed
feat: add solutions to lc problems: No.2248,2249
* No.2248.Intersection of Multiple Arrays * No.2249.Count Lattice Points Inside a Circle
1 parent 1d3ad09 commit 0532cec

File tree

14 files changed

+482
-266
lines changed

14 files changed

+482
-266
lines changed

‎solution/2200-2299/2248.Intersection of Multiple Arrays/README.md‎

Lines changed: 134 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ nums[0] = [<em><strong>3</strong></em>,1,2,<em><strong>4</strong></em>,5],nums
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:计数**
46+
47+
遍历数组 `nums`,对于每个数组 `arr`,统计数组 `arr` 中每个数字出现的次数,然后遍历计数数组,统计出现次数等于数组 `nums` 的长度的数字,即为答案。
48+
49+
时间复杂度 $O(N),ドル空间复杂度 $O(1000)$。其中 $N$ 为数组 `nums` 中数字的总数。
50+
4551
<!-- tabs:start -->
4652

4753
### **Python3**
@@ -52,11 +58,24 @@ nums[0] = [<em><strong>3</strong></em>,1,2,<em><strong>4</strong></em>,5],nums
5258
class Solution:
5359
def intersection(self, nums: List[List[int]]) -> List[int]:
5460
cnt = [0] * 1001
55-
for num in nums:
56-
for v in num:
57-
cnt[v] += 1
58-
n = len(nums)
59-
return [i for i, v in enumerate(cnt) if v == n]
61+
for arr in nums:
62+
for x in arr:
63+
cnt[x] += 1
64+
return [x for x, v in enumerate(cnt) if v == len(nums)]
65+
```
66+
67+
```python
68+
class Solution:
69+
def intersection(self, nums: List[List[int]]) -> List[int]:
70+
cnt = Counter()
71+
ans = []
72+
for arr in nums:
73+
for x in arr:
74+
cnt[x] += 1
75+
if cnt[x] == len(nums):
76+
ans.append(x)
77+
ans.sort()
78+
return ans
6079
```
6180

6281
### **Java**
@@ -67,34 +86,37 @@ class Solution:
6786
class Solution {
6887
public List<Integer> intersection(int[][] nums) {
6988
int[] cnt = new int[1001];
70-
for (int[] num : nums) {
71-
for (int i : num) {
72-
cnt[i]++;
89+
for (var arr : nums) {
90+
for (int x : arr) {
91+
++cnt[x];
7392
}
7493
}
7594
List<Integer> ans = new ArrayList<>();
76-
for (int i = 1; i <=1000; i++) {
77-
if (cnt[i] == nums.length) {
78-
ans.add(i);
95+
for (int x = 0; x <1001; ++x) {
96+
if (cnt[x] == nums.length) {
97+
ans.add(x);
7998
}
8099
}
81100
return ans;
82101
}
83102
}
84103
```
85104

86-
### **TypeScript**
87-
88-
```ts
89-
function intersection(nums: number[][]): number[] {
90-
const n = nums.length;
91-
let ans = nums[0];
92-
for (let i = 1; i < n && ans.length; i++) {
93-
const cur = new Set(nums[i]);
94-
// get intersect
95-
ans = ans.filter(v => cur.has(v));
105+
```java
106+
class Solution {
107+
public List<Integer> intersection(int[][] nums) {
108+
Map<Integer, Integer> cnt = new HashMap<>();
109+
List<Integer> ans = new ArrayList<>();
110+
for (var arr : nums) {
111+
for (int x : arr) {
112+
if (cnt.merge(x, 1, Integer::sum) == nums.length) {
113+
ans.add(x);
114+
}
115+
}
116+
}
117+
Collections.sort(ans);
118+
return ans;
96119
}
97-
return ans.sort((a, b) => a - b);
98120
}
99121
```
100122

@@ -104,15 +126,37 @@ function intersection(nums: number[][]): number[] {
104126
class Solution {
105127
public:
106128
vector<int> intersection(vector<vector<int>>& nums) {
107-
vector<int> cnt(1001);
108-
for (auto& num : nums)
109-
for (int v : num)
110-
++cnt[v];
111-
int n = nums.size();
129+
int cnt[1001]{};
130+
for (auto& arr : nums) {
131+
for (int& x : arr) {
132+
++cnt[x];
133+
}
134+
}
135+
vector<int> ans;
136+
for (int x = 0; x < 1001; ++x) {
137+
if (cnt[x] == nums.size()) {
138+
ans.push_back(x);
139+
}
140+
}
141+
return ans;
142+
}
143+
};
144+
```
145+
146+
```cpp
147+
class Solution {
148+
public:
149+
vector<int> intersection(vector<vector<int>>& nums) {
150+
unordered_map<int, int> cnt;
112151
vector<int> ans;
113-
for (int i = 1; i < 1001; ++i)
114-
if (cnt[i] == n)
115-
ans.push_back(i);
152+
for (auto& arr : nums) {
153+
for (int& x : arr) {
154+
if (++cnt[x] == nums.size()) {
155+
ans.push_back(x);
156+
}
157+
}
158+
}
159+
sort(ans.begin(), ans.end());
116160
return ans;
117161
}
118162
};
@@ -121,20 +165,71 @@ public:
121165
### **Go**
122166

123167
```go
124-
func intersection(nums [][]int) []int {
125-
cnt := make([]int, 1001)
126-
for _, num := range nums {
127-
for _, v := range num {
128-
cnt[v]++
168+
func intersection(nums [][]int) (ans[]int) {
169+
cnt := [1001]int{}
170+
for _, arr := range nums {
171+
for _, x := range arr {
172+
cnt[x]++
129173
}
130174
}
131-
var ans []int
132-
for i, v := range cnt {
175+
for x, v := range cnt {
133176
if v == len(nums) {
134-
ans = append(ans, i)
177+
ans = append(ans, x)
135178
}
136179
}
137-
return ans
180+
return
181+
}
182+
```
183+
184+
```go
185+
func intersection(nums [][]int) (ans []int) {
186+
cnt := map[int]int{}
187+
for _, arr := range nums {
188+
for _, x := range arr {
189+
cnt[x]++
190+
if cnt[x] == len(nums) {
191+
ans = append(ans, x)
192+
}
193+
}
194+
}
195+
sort.Ints(ans)
196+
return
197+
}
198+
```
199+
200+
### **TypeScript**
201+
202+
```ts
203+
function intersection(nums: number[][]): number[] {
204+
const cnt = new Array(1001).fill(0);
205+
for (const arr of nums) {
206+
for (const x of arr) {
207+
cnt[x]++;
208+
}
209+
}
210+
const ans: number[] = [];
211+
for (let x = 0; x < 1001; x++) {
212+
if (cnt[x] === nums.length) {
213+
ans.push(x);
214+
}
215+
}
216+
return ans;
217+
}
218+
```
219+
220+
```ts
221+
function intersection(nums: number[][]): number[] {
222+
const cnt = new Array(1001).fill(0);
223+
const ans: number[] = [];
224+
for (const arr of nums) {
225+
for (const x of arr) {
226+
if (++cnt[x] == nums.length) {
227+
ans.push(x);
228+
}
229+
}
230+
}
231+
ans.sort((a, b) => a - b);
232+
return ans;
138233
}
139234
```
140235

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /