Code Monkey home page Code Monkey logo

Comments (3)

PisecesPeng avatar PisecesPeng commented on September 26, 2024

解题思路

  • 在假定数组已有序的情况下, 可以直接使用双指针遍历

代码

// 排序 基于给定的数组已排序
// Arrays.sort(nums1); Arrays.sort(nums2);
private static int[] func(int[] nums1, int[] nums2) {
    int length1 = nums1.length, length2 = nums2.length;
    int index1 = 0, index2 = 0, indexRes = 0;

    // 存放结果的数组
    int[] res = new int[length1 > length2 ? length1 : length2];
    // 当有一个数组结束遍历, 则终止全部遍历
    // 双指针, 判断指针指定的值大小, 来挪动指针位置
    while (index1 < length1 && index2 < length2) {
        if (nums1[index1] == nums2[index2]) {
            res[indexRes++] = nums1[index1];
            index1++;
            index2++;
        }
        else if (nums1[index1] > nums2[index2]) {
            index2++;
        }
        else index1++;
    }
    // 截取结果
    return Arrays.copyOf(res, indexRes);
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 26, 2024

LeetCode题解

解题思路

  • 初始两个指针指向数组头部,
  • 每次比较两指针指向的数组大小,
  • 将较小数字的指针右移一位, 若两数相等则记录, 并两指针均右移一位,
  • 当至少有一个指针超出范围, 遍历结束.

代码

public static int[] func(int[] nums1, int[] nums2) {
    Arrays.sort(nums1);
    Arrays.sort(nums2);
    int length1 = nums1.length, length2 = nums2.length;
    int[] intersection = new int[Math.min(length1, length2)];
    int index1 = 0, index2 = 0, index = 0;
    while (index1 < length1 && index2 < length2) {
        if (nums1[index1] < nums2[index2]) {
            index1++;
        } else if (nums1[index1] > nums2[index2]) {
            index2++;
        } else {
            intersection[index] = nums1[index1];
            index1++;
            index2++;
            index++;
        }
    }
    return Arrays.copyOfRange(intersection, 0, index);
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 26, 2024

LeetCode题解

解题思路

  • 先将第一个数组中的所有数字及其出现次数记录在Map中,
  • 遍历第二个数组, 并与Map匹配, 得出交集.

代码

public static int[] func(int[] nums1, int[] nums2) {
    // 可以通过遍历较短的数组, 来降低空间复杂度
    if (nums1.length > nums2.length) {
        return func(nums2, nums1);
    }
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int num : nums1) {
        int count = map.getOrDefault(num, 0) + 1;
        map.put(num, count);
    }
    int[] intersection = new int[nums1.length];
    int index = 0;
    for (int num : nums2) {
        int count = map.getOrDefault(num, 0);
        if (count > 0) {
            intersection[index++] = num;
            count--;
            if (count > 0) {
                map.put(num, count);
            } else {
                map.remove(num);
            }
        }
    }
    return Arrays.copyOfRange(intersection, 0, index);
}

from pisecespeng.record.me.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.