原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/
题目:
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
Follow up:
题解:
AC Java:
1 public class Solution { 2 public int[] intersect(int[] nums1, int[] nums2) { 3 Arrays.sort(nums1); 4 Arrays.sort(nums2); 5 int i = 0; 6 int j = 0; 7 List<Integer> res = new ArrayList<Integer>(); 8 while(i<nums1.length && j<nums2.length){ 9 if(nums1[i] < nums2[j]){ 10 i++; 11 }else if(nums1[i] > nums2[j]){ 12 j++; 13 }else{ 14 res.add(nums1[i]); 15 i++; 16 j++; 17 } 18 } 19 20 int [] resArr = new int[res.size()]; 21 int k = 0; 22 for(int num : res){ 23 resArr[k++] = num; 24 } 25 return resArr; 26 } 27 }
LeetCode Intersection of Two Arrays II
原文:http://www.cnblogs.com/Dylan-Java-NYC/p/6255195.html