您的位置:首页 > 编程语言 > Java开发

[LeetCode]Intersection of Two Arrays II(Java)

2016-08-26 13:53 489 查看
感觉我自己这个应该是最好的了

<span style="font-size:18px;">public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> interList = new ArrayList<Integer>();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0,j = 0;
while(i<nums1.length&&j<nums2.length){
if(nums1[i] == nums2[j]){
interList.add(nums1[i]);
i++;
j++;
}else if(nums1[i]>nums2[j]){
j++;
}else{
i++;
}
}
int[] interArray = new int[interList.size()];
Iterator it = interList.iterator();
i=0;
while(it.hasNext()){
interArray[i]=(Integer)(it.next());
i++;
}
return interArray;
}
}</span>2016/8/26
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode java