Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

📢 preface

🚀 Algorithm 🚀
  • 🌲 punch in an algorithm every day, which is not only a learning process, but also a sharing process 😜
  • 🌲 tip: the programming languages used in this column are C# and Java
  • 🌲 to maintain a state of learning every day, let us work together to become a god of algorithms 🧐!
  • 🌲 today is the force button algorithm continues to punch card 24 days 🎈!
🚀 Algorithm 🚀

🌲 Example of original problem

You are given two non-descending arrays of integers, nums1 and nums2, and two integers, m and n, representing the number of elements in nums1 and nums2, respectively.

Please merge nums2 into nums1 so that the merged array is also in non-descending order.

Note: Finally, the merged array should not be returned by the function, but stored in the array nums1. To deal with this, nums1 has an initial length of m + n, where the first m elements represent the elements that should be combined and the last n elements are 0 and should be ignored. Nums2 has a length of n.

The sample1: Input: nums1 = [1.2.3.0.0.0], m = 3, nums2 = [2.5.6], n = 3Output:1.2.2.3.5.6] Explanation: need to merge [1.2.3] and [2.5.6]. The combined result is [1.2.2.3.5.6], where the elements in italic bold are nums1.Copy the code
The sample2: Input: nums1 = [1], m = 1, nums2 = [], n = 0Output:1] Explanation: need to merge [1] and []. The combined result is [1].Copy the code
The sample3: Input: nums1 = [0], m = 0, nums2 = [1], n = 1Output:1The arrays to merge are [] and [].1]. The combined result is [1]. Notice, because m is equal to0So there are no elements in nums1. Only existing in NUMs10Just to make sure that the merge results can be safely stored in NUMs1.Copy the code

Tip:

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -109 <= nums1[i], nums2[j] <= 109

🌻C# method: sort directly after merge

Thinking analytical

The final goal is to merge two ordered arrays

Add nums2 to nums1; sort sort nums1

Code:

public class Solution {
public void Merge(int[] nums1, int m, int[] nums2, int n) {
    for(inti=m; i<m+n; i++) { nums1[i]=nums2[i-m]; } Array.Sort(nums1); }}Copy the code

The execution result

By execution time:220Ms, in all C# beat 87.01% of users in submissionMemory consumption:30.7MB, in all CBeat 5.29% of users in # commit
Copy the code

Complexity analysis

Time: O(n) Space: O(1)
Copy the code

🌻Java method 1: Sort after merge

Nums 1 = nums 2; nums 1 = nums 1; nums 2 = nums 1; nums 2 = nums 1;

Code:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i != n; ++i) {
            nums1[m + i] = nums2[i];
        }
        Arrays.sort(nums1);
    }
}
Copy the code

The execution result

By execution time:1Ms, beat out all Java commits19.05% user memory consumption:38.8MB, beat out all Java commits5.15% of the userCopy the code

Complexity analysis

Time: O((m+n)log(m+n)) Space: O(log(m+n))Copy the code

🌻Java Method 2: dual Pointers

Thinking analytical

Method 1 does not take advantage of the fact that nums 1 and nums 2 have already been sorted. To take advantage of this property, we can use the two-pointer method.

This method treats the two arrays as queues, and each time it takes a smaller number from the head of each array and puts it into the result. As shown in the following animation:

Code:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = 0, p2 = 0;
        int[] sorted = new int[m + n];
        int cur;
        while (p1 < m || p2 < n) {
            if (p1 == m) {
                cur = nums2[p2++];
            } else if (p2 == n) {
                cur = nums1[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                cur = nums1[p1++];
            } else {
                cur = nums2[p2++];
            }
            sorted[p1 + p2 - 1] = cur;
        }
        for (int i = 0; i ! = m + n; ++i) { nums1[i] = sorted[i]; }}}Copy the code

The execution result

By execution time:0Ms, beat out all Java commits100% user memory consumption:38.7MB, beat out all Java commits15.12% of the userCopy the code

Complexity analysis

Time complexity: O(m+ N) Space complexity: O(m+n)Copy the code

💬 summary

  • Today is the 24th day of the buckle algorithm clocking!
  • The article USES theC# andJavaTwo programming languages to solve the problem
  • Some methods are also written by the god of reference force buckle, and they are also shared while learning, thanks again to the algorithm masters
  • That’s the end of today’s algorithm sharing, see you tomorrow!