【LintCode】计算两个数的交集(一)

问题分析:
既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较。
问题求解:
public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < nums1.length; i++) {
set1.add(nums1[i]);
}
for (int i = 0; i < nums2.length; i++) {
set2.add(nums2[i]);
}
Iterator<Integer> it = set2.iterator();
while (it.hasNext()) {
int num2 = it.next();
if(set1.contains(num2)){
list.add(num2);
}
}
int[] inter = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
inter[i] = list.get(i);
}
return inter;
}
}
【LintCode】计算两个数的交集(一)的更多相关文章
- [LintCode]计算两个数的交集(二)
问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 ...
- [LintCode]计算两个数的交集(一)
问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an inte ...
- 【LintCode】计算两个数的交集(二)
问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- grep和map计算两个集合交集、并集、补集
#!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集###### ...
- python计算两个数的百分比
a和b是整数,计算a/b的百分比 a=3 b=7 a=float(a) b=float(b) 保留百分比后2位小数 print "%.2f%%" % (a/b*100) '42. ...
- 用shell脚本 计算两个数的加减乘除取余
#! /bin/bash # read -p '请输入数:' a //输入 read -p '请输入数:' b echo '$a+$b=' $(( a + b )) //输出 echo '$a-$b= ...
- PHP计算两个时间段是否有交集(边界重叠不算)
优化前的版本: /** * PHP计算两个时间段是否有交集(边界重叠不算) * * @param string $beginTime1 开始时间1 * @param string $endTime1 ...
- spark计算两个DataFrame的差集、交集、合集
spark 计算两个dataframe 的差集.交集.合集,只选择某一列来对比比较好.新建两个 dataframe : import org.apache.spark.{SparkConf, Spar ...
随机推荐
- 使用exp进行SQL报错注入
0x01 前言概述 好消息好消息-作者又在MySQL中发现了一个Double型数据溢出.如果你想了解利用溢出来注出数据,你可以读一下作者之前发的博文:BIGINT Overflow Error bas ...
- Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别
通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是 /** * Parses the string argument as a signed integer in the ...
- GridView EmptyDataTemplate 动态显示
以下语句加在GridView.DataBind()之后: Table GridViewTable = ((Table)gvGridView.Controls[]); if (!isSearch) (( ...
- BZOJ 1251: 序列终结者
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3773 Solved: 1579 [Submit][Status][Dis ...
- HDU 1166 敌兵布阵
B - 敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 50个查询系列-第13个查询:把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
UPDATE tblscore SET tblscore.Score= ( -- 这里开始算叶平的平均值 SELECT AVG(tt.aa) FROM ( SELECT tblscore.Score ...
- centos 命令集合
链接: http://www.cnblogs.com/zitsing/archive/2012/05/02/2479009.html http://www.centoscn.com/CentOS/he ...
- 获取iTextSharp 的image 报错
获取itextsharp类库的image对象的时候报错 outofmemory .经过艰苦的测试发现jpeg类型是可行的的 iTextSharp.text.Image je = iTextShar ...
- python 测试驱动开发的简单例子
一.需求分析 需求:一个类 MyClass,有两个功能:add, sub 1.先功能设计 # myclass.py class MyClass(object): # 加法 def add(self): ...
- C# FTP上传
public class FtpHelper { public FtpHelper(string p_url, string p_user, string p_password) { if (!p_u ...