Re: [閒聊] 每日leetcode
看板Marginalman作者sustainer123 (caster )時間1年前 (2024/03/10 16:57)推噓2(2推 0噓 2→)留言4則, 4人參與討論串31/1548 (看更多)
※ 引述《DJYOSHITAKA (franchouchouISBEST)》之銘言:
: 最近好多easy 又水了一天
: 亂寫一通
: 349. Intersection of Two Arrays
: Given two integer arrays nums1 and nums2, return an array of their
: intersection. Each element in the result must be unique and you may return
: the result in any order.
: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
: vector<int> cnt(1001,0);
: unordered_set<int> a;
: unordered_set<int> b;
: vector<int> ans;
: for(auto i : nums1)
: {
: a.insert(i);
: }
: for(auto i : nums2)
: {
: b.insert(i);
: }
: for(auto i : a)
: {
: cnt[i]++;
: }
: for(auto i : b)
: {
: cnt[i]--;
: if(cnt[i] == 0)
: {
: ans.push_back(i);
: }
: }
: return ans;
: }
思路:取交集
Python Code:
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return set(nums1) & set(nums2)
又是ez 不過速度有點慢 等等看一下解答
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.9.228 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1710061048.A.627.html
→
03/10 16:58,
1年前
, 1F
03/10 16:58, 1F
推
03/10 17:08,
1年前
, 2F
03/10 17:08, 2F
→
03/10 17:11,
1年前
, 3F
03/10 17:11, 3F
推
03/10 17:31,
1年前
, 4F
03/10 17:31, 4F
討論串 (同標題文章)