Skip to content

Commit

Permalink
feat: 349.intersection-of-two-arrays add Python3 implementation (azl3…
Browse files Browse the repository at this point in the history
  • Loading branch information
ybian19 authored and azl397985856 committed Aug 26, 2019
1 parent 6d11fe9 commit 532c363
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions problems/349.intersection-of-two-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ The result can be in any order.

## 代码

* 语言支持:JS, Python

Javascript Code:

```js
/*
* @lc app=leetcode id=349 lang=javascript
Expand All @@ -47,31 +52,31 @@ The result can be in any order.
* Testcase Example: '[1,2,2,1]\n[2,2]'
*
* Given two arrays, write a function to compute their intersection.
*
*
* Example 1:
*
*
*
*
* Input: nums1 = [1,2,2,1], nums2 = [2,2]
* Output: [2]
*
*
*
*
*
*
* Example 2:
*
*
*
*
* Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
* Output: [9,4]
*
*
*
*
* Note:
*
*
*
*
* Each element in the result must be unique.
* The result can be in any order.
*
*
*
*
*
*
*
*
*/
/**
* @param {number[]} nums1
Expand Down Expand Up @@ -101,3 +106,21 @@ var intersection = function(nums1, nums2) {
};
```

Python Code:

```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
visited, result = {}, []
for num in nums1:
visited[num] = num
for num in nums2:
if num in visited:
result.append(num)
visited.pop(num)
return result

# 另一种解法:利用 Python 中的集合进行计算
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return set(nums1) & set(nums2)
```

0 comments on commit 532c363

Please sign in to comment.