349. Intersection of Two Arrays#2(Done)

来源:互联网 发布:karunesh 知乎 编辑:程序博客网 时间:2024/06/04 23:34

Solution

public class Solution {    public int[] intersection(int[] nums1, int[] nums2) {        Arrays.sort(nums1);        Arrays.sort(nums2);        int[] store = new int[nums1.length];        int i = 0;        int j = 0;        int count = 0;        while (i < nums1.length && j < nums2.length) {            if (nums1[i] < nums2[j]) {                i++;            } else if (nums1[i] > nums2[j]) {                j++;            } else {                if (count == 0 || store[count - 1] != nums1[i]) {                    store[count++] = nums1[i];                }                i++;                j++;            }        }        int[] result = new int[count];        for (int k = 0; k < count; k++) {            result[k] = store[k];        }        return result;    }}

Problem#1
不熟悉hashTable

0 0
原创粉丝点击