leetcode 349. Intersection of Two Arrays 1
Description
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
Each element in the result must be unique.
The result can be in any order.
My solution
- 自然的想法是把nums1的每个数和nums2的每个数对比,复杂度为O(nm).如果排好序的呢? 那就是nlgn和mlgm,一般情况下这个方式更小一点.
- 考虑到有很多重复数字, 并且问题的根本在于判断"共有元素",所以很容易联想到python方式, 把数组专为set,用 in / not in判断.没看过源码,也不知道这个过程复杂度是怎样的 ==
下为先排序的方式:
class Solution {
public:vector<int> intersection(vector<int> &nums1, vector<int> &nums2) {vector<int> res;sort(nums1.begin(), nums1.end());sort(nums2.begin(), nums2.end());int i = 0, j = 0;while (i < nums1.size() && j < nums2.size()) {if (nums1[i] < nums2[j]) ++i;else if (nums1[i] > nums2[j]) ++j;else {res.push_back(nums1[i]);++i;++j;while (nums1[i] == nums1[i - 1]) ++i;while (nums2[j] == nums2[j - 1]) ++j;}}return res;}
};
Discuss
using c++ set
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {set<int> s(nums1.begin(), nums1.end());vector<int> out;for (int x : nums2)if (s.erase(x))out.push_back(x);return out;
}
急需看完c++ primer 啊!!
Reference
- leetcode
- c++ set
leetcode 349. Intersection of Two Arrays 1相关推荐
- leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- LeetCode 350. Intersection of Two Arrays II
题目: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
- LeetCode之Intersection of Two Arrays
1.题目 Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
- 349. Intersection of Two Arrays 两个数组的交集
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例 2: 输入:nums1 = [4,9,5], ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- LeetCode -349 两个数组的交集
难度:简单 给定两个数组 nums1 和 nums2 ,返回它们的交集 .输出结果中的每个元素一定是唯一 的.我们可以不考虑输出结果的顺序 . 题目链接 LeetCode -349 两个数组的交集 S ...
- 53 两数组的交集(Intersection of Two Arrays)
文章目录 1 题目 2 解决方案 2.1 思路 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 排序+合并的方式 3.2 二分搜索的方式 3.3 哈希表的方式 1 题目 题目:两数组的交集( ...
- Leetcode: Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
最新文章
- laravel和dingoapi的结合使用
- Uva10285 Longest Run on a Snowboard
- php实现sql server数据导入到mysql数据库_SQL Server数据库导入MySQL数据库的体验_MySQL...
- arailsdemo 1
- 最新StrongShop跨境电商系统源码+支持多语言
- 计算机实验室安全员责任书,实验室人员安全责任书
- X-VECTORS: ROBUST DNN EMBEDDINGS FOR SPEAKER RECOGNITION论文翻译
- android模拟器克隆app,易语言一键克隆/启动安卓模拟器
- 微信测试号实现微信网页的分享
- Drupal独到的编程思想
- 计算机网络安全讲座心得,学习信息安全心得体会
- 傅雷家书与互联网从业者的思考
- kafka自定义生产者分区器、自定义消费者分区器
- linux 进文字界面,CentOS安装后进入时文字界面,不知如何用命令,求解
- 痘印服务器维护,脸上有痘印怎么弄才能消除
- BS架构和CS架构 + Tomcat安装及配置
- 为什么需要功能需求设计说明书
- Maemo 平台升级:Diablo
- JS实现购物车的基本功能
- 模型剪枝学习笔记--SlimYOLOv3:Narrower,Faster and Better for Real-Time UAV Application