作者sustainer123 (caster )
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Tue Jul 2 08:12:43 2024
https://leetcode.com/problems/intersection-of-two-arrays-ii
350. Intersection of Two Arrays II
給定兩array 求交集
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]
Explanation: [9,4] is also accepted.
Constraints:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
思路:
counter計算各元素出現次數 然後求交集
Python Code:
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list((Counter(nums1) & Counter(nums2)).elements())
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.160.111 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1719879166.A.05C.html
推 Furina: 大師 07/02 08:13
→ sixB: 不是 太早了吧 07/02 08:15
→ sixB: 到底多卷 07/02 08:15
→ DJYOMIYAHINA: 法國我 07/02 08:17
→ sustainer123: 我4ez守門員 07/02 08:19
推 SecondRun: 不准用內建函式 07/02 08:26
推 smart0eddie: 大師 07/02 08:33