Array 6 - Intersection of Two Arrays II

Question URL: https://leetcode.cn/leetbook/read/top-interview-questions-easy/x2y0c2/

public class Solution {
    public int[] Intersect(int[] nums1, int[] nums2) {
        int[] nums = new int[1000];
        int k = 0;
        nums[k] = -1;
        for (int i = 0; i < nums1.Length; i++)
        {
            for (int j = 0; j < nums2.Length; j++)
            {
                if (nums1[i] == nums2[j])
                {
                    nums[k + 1] = -1;
                    nums[k] = nums1[i];
                    k++;
                    nums1[i] = -2;
                    nums2[j] = -2;
                    break;
                }
            }
        }
        int len = 0;
        for (int i = 0; nums[i] != -1; i++)
        {
            len++;
        }
        int[] numsEnd = new int[len];
        for (int i = 0; nums[i] != -1; i++)
        {
            numsEnd[i] = nums[i];
        }
        return numsEnd;
    }
}

Guess you like

Origin blog.csdn.net/oyqho/article/details/129597787