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 532c363

Browse files
ybian19azl397985856
authored andcommitted
feat: 349.intersection-of-two-arrays add Python3 implementation (azl397985856#147)
1 parent 6d11fe9 commit 532c363

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

‎problems/349.intersection-of-two-arrays.md‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ The result can be in any order.
3232
3333

3434
## 代码
35+
36+
* 语言支持:JS, Python
37+
38+
Javascript Code:
39+
3540
```js
3641
/*
3742
* @lc app=leetcode id=349 lang=javascript
@@ -47,31 +52,31 @@ The result can be in any order.
4752
* Testcase Example: '[1,2,2,1]\n[2,2]'
4853
*
4954
* Given two arrays, write a function to compute their intersection.
50-
*
55+
*
5156
* Example 1:
52-
*
53-
*
57+
*
58+
*
5459
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
5560
* Output: [2]
56-
*
57-
*
58-
*
61+
*
62+
*
63+
*
5964
* Example 2:
60-
*
61-
*
65+
*
66+
*
6267
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
6368
* Output: [9,4]
64-
*
65-
*
69+
*
70+
*
6671
* Note:
67-
*
68-
*
72+
*
73+
*
6974
* Each element in the result must be unique.
7075
* The result can be in any order.
71-
*
72-
*
73-
*
74-
*
76+
*
77+
*
78+
*
79+
*
7580
*/
7681
/**
7782
* @param {number[]} nums1
@@ -101,3 +106,21 @@ var intersection = function(nums1, nums2) {
101106
};
102107
```
103108

109+
Python Code:
110+
111+
```python
112+
class Solution:
113+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
114+
visited, result = {}, []
115+
for num in nums1:
116+
visited[num] = num
117+
for num in nums2:
118+
if num in visited:
119+
result.append(num)
120+
visited.pop(num)
121+
return result
122+
123+
# 另一种解法:利用 Python 中的集合进行计算
124+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
125+
return set(nums1) & set(nums2)
126+
```

0 commit comments

Comments
(0)

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