两数组的交 II
来源:互联网 发布:java windows api 编辑:程序博客网 时间:2024/05/16 09:25
问题描述:计算两个数组的交
样例
nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, 返回 [2,2]
.
实验代码:
class Solution {
public:
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
// Write your code here
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int i=0, j=0;
int num1 = nums1.size();
int num2 = nums2.size();
vector<int> v;
while(i<num1 && j<num2)
{if(nums1[i] == nums2[j])
{
v.push_back(nums1[i]);
i++;
j++;
}
else if(nums1[i] > nums2[j])
{ j++;}
else
{ i++;}
}
return v;
}
};
个人感想:else if的用法。
- 两数组的交 II
- 两数组的交 II
- 两数组的交II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- 两数组的交 II
- LintCode:两数组的交 II
- LintCode 548 两数组的交 II
- LintCode【两数组的交II】
- Android中使用Spinner来实现图标文字并存的二级联动
- android Edittext 设置行间距
- Ubuntu操作系统下Pixhawk原生固件PX4的编译方法
- Hdu2162 Add ‘em
- Java中方法的重写(override)和重载(overload)的使用规则总结:
- 两数组的交 II
- 基于Python库surprise的电影推荐系统
- HTML5 eclipse中写java,html,css,javascript代码提示
- 哈夫曼编码,应用模板特化递归+bind函数绑定
- c++ primer
- Linux_正则表达式
- Linux tar 命令 压缩、解压
- [DuiLib教程2]DuiLib第一个测试Demo的注释
- 【论文阅读】 Enhancing Video Event Recognition Using Automatically Constructed Semantic-Visual Knowledge