Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (虛構史學家)時間1年前 (2024/07/02 17:37), 編輯推噓3(300)
留言3則, 3人參與, 1年前最新討論串436/1554 (看更多)
※ 引述《smart0eddie (smart0eddie)》之銘言: : 20240702 : 350. Intersection of Two Arrays II : Given two integer arrays nums1 and nums2, return an array of their : intersection. Each element in the result must appear as many times as it : shows in both arrays and you may return the result in any order. HashMap Code: use std::collections::HashMap; impl Solution { pub fn intersect(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> { let mut nums1_hash = HashMap::new(); let mut nums2_hash = HashMap::new(); for &num in &nums1 { *nums1_hash.entry(num).or_insert(0) += 1; } for &num in &nums2 { *nums2_hash.entry(num).or_insert(0) += 1; } let mut result = Vec::new(); for (&num, &count) in &nums1_hash { if let Some(&count2) = nums2_hash.get(&num) { let min_count = count.min(count2); for _ in 0..min_count { result.push(num); } } } result } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.48.170 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1719913061.A.A30.html

07/02 17:38, 1年前 , 1F
大師
07/02 17:38, 1F

07/02 17:41, 1年前 , 2F
大師
07/02 17:41, 2F

07/02 17:41, 1年前 , 3F
大濕
07/02 17:41, 3F
文章代碼(AID): #1cWyfbem (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cWyfbem (Marginalman)