Code Monkey home page Code Monkey logo

Comments (3)

PisecesPeng avatar PisecesPeng commented on September 7, 2024

解题思路

  • 新建空数组, 双指针从前往后依次遍历两数组, 将值按顺序填充.

代码

public static void func(int[] nums1, int m, int[] nums2, int n) {
    // 新构造一个数组
    int[] nums = new int[m + n];
    int i = 0, j = 0, index = 0;
    while (i < m || j < n) {
        // 双指针遍历两数组, 每次选择小值放入新数组中
        if (j >= n) {
            while (i < m) nums[index++] = nums1[i++];
            break;
        }
        if (i >= m) {
            while (j < n) nums[index++] = nums2[j++];
            break;
        }
        if (nums1[i] < nums2[j])
            nums[index++] = nums1[i++];
        else
            nums[index++] = nums2[j++];
    }
    System.arraycopy(nums,0,nums1,0,nums.length);
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 7, 2024

LeetCode题解

解题思路

  • 将两数组合并, 再排序

代码

public static void func(int[] nums1, int m, int[] nums2, int n) {
    for (int i = 0; i != n; ++i) {
        nums1[m + i] = nums2[i];
    }
    Arrays.sort(nums1);
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 7, 2024

LeetCode题解

解题思路

  • 逆向双指针,
  • 正向双指针之所以需要开辟新的数组, 就是因为元素覆盖的问题,
  • 逆向双指针则不需要考虑这个问题.

代码

public static void func(int[] nums1, int m, int[] nums2, int n) {
    int p1 = m - 1, p2 = n - 1;
    int tail = m + n - 1;
    int cur;
    while (p1 >= 0 || p2 >= 0) {
        if (p1 == -1) {
            cur = nums2[p2--];
        } else if (p2 == -1) {
            cur = nums1[p1--];
        } else if (nums1[p1] > nums2[p2]) {
            cur = nums1[p1--];
        } else {
            cur = nums2[p2--];
        }
        nums1[tail--] = cur;
    }
}

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.