leetcode349. 两个数组的交集(思路+详解)
生活随笔
收集整理的這篇文章主要介紹了
leetcode349. 两个数组的交集(思路+详解)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:題目
二:上碼
class Solution { public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {/**思路:本題我們采用的哈希表數據結構是unordered_set,沒有用數組因為我們不確定給出的數組中數值的范圍如果 強上會出現空間的大量浪費 */unordered_set<int> s,s1;//底層實現是哈希表,效率較高,而且也可以去重,但無序unordered_set<int>:: iterator st;vector<int> v;for(int i = 0; i < nums1.size(); i++){s.insert(nums1[i]);}for(int i = 0; i < nums2.size(); i++){if(s.find(nums2[i]) != s.end()){s1.insert(nums2[i]);}}for(st = s1.begin(); st != s1.end(); st++){v.push_back(*st);}return v;} };總結
以上是生活随笔為你收集整理的leetcode349. 两个数组的交集(思路+详解)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 木瓜、牛奶什么时候吃丰胸效果最好
- 下一篇: leetcode202. 快乐数