Likou Brushing Hundred Days Plan Day11 Merge two ordered arrays C#

learning target:

I will continue to update my unique algorithm ideas, hoping to bring you different thinking expansion!
If you find it helpful, please like, follow and support!
Your encouragement is what keeps me going!
! ! !

Likou Question Bank Question 88 Official Link

Learning Content:

Merge two sorted arrays

给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。

请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。

注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。

Example 1:

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: Need to combine [1,2,3] and [2,5,6].
The combined result is [1,2,2,3,5,6], where the elements in nums1 are marked in bold italics.
Example 2:

Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: Need to combine [1] and [].
The combined result is [1].
Example 3:

Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays to be merged are [] and [1].
The combined result is [1].
Note that since m = 0, there are no elements in nums1. The only 0 in nums1 is to ensure that the merged result can be stored in nums1 smoothly.

提示:

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

进阶:你可以设计实现一个时间复杂度为 O(m + n) 的算法解决此问题吗?

Source: LeetCode
Link: https://leetcode-cn.com/problems/merge-sorted-array

study-time:

2022.1.20


Learning output:

idea one
insert image description here

Problem- solving ideas
Insertion method
1. We set the two pointers as the starting addresses of the two arrays, the arrays are sorted from small to large, so we use the first array as the base array
2. We compare all the first arrays element and the element pointed to by the pointer of the second array, find a value greater than the element pointed to by the pointer of array 2. After finding it, we insert the element pointed to by the pointer of array 2 into this position of array 1. If it is not found, it proves that the elements pointed to by the pointer of array 2 are larger than those of array 1, then insert the elements pointed to by the pointer of array 2 into the last data of array 1.
3. After the pointer of array 2 is traversed, the values ​​of array 2 are also all Insert into array 1.
4. This algorithm needs to traverse array 1 once every time. In fact, the time complexity is relatively high, but we have made a small optimization. Each time, we will only judge from the position where array 1 is inserted.
Because array 2 is arranged in ascending order, the value after array 2 must be larger than the value at the insertion position. To compare it with the smaller value of array 1 will only waste time.
insert image description here

public class Solution {
    
    
    public void Merge(int[] nums1, int m, int[] nums2, int n) {
    
    
    
        int num1Cur_Length=m;
        int lastInsert = 0;
        if(m==0){
    
    
            for(int i=0;i<n;i++){
    
    
                nums1[i]=nums2[i];
            }
        }else{
    
    
        for(int i=0;i<n;i++){
    
    
            //将2全部放入1
            for(int j=0;j<num1Cur_Length;j++){
    
    
                if(nums1[j]>nums2[i]){
    
    
                    nums1=InsertInto(nums1,j,nums2[i]);
                    lastInsert=j;
                    num1Cur_Length++;
                    break;
                }else if(j==num1Cur_Length-1) {
    
    
                    nums1[num1Cur_Length++]=nums2[i];
                    lastInsert=num1Cur_Length;
                    break;
                }
            }
        }
        }

        
    }

    int[] InsertInto(int[] num,int index,int value){
    
    
            for(int i=num.Length - 1;i>index;i--){
    
    
                num[i]=num[i-1];
            }
            num[index]=value;

            return num;
    }
}

Idea 2 *
insert image description here

Problem- solving ideas
Double pointer method
1. First, we define two pointers, pointing to the last element of the two arrays (array 1 points to the position of data length -1), because the last element of the array is the largest, and the back of array 1 is all is blank, so we can row from back to front in array 1, from large to small. You also need to specify an indexlast pointer, pointing to the last element of array 1.
2. That is, compare the values ​​pointed to by the two pointers, put the larger one at the indexlast pointer position of array 1, then move the two pointers and the indexlast pointer to the left at the same time, and continue to compare
3. Repeat 2 until all the arrays are compared, then Put all the other arrays into it
insert image description here

public class Solution {
    
    
    public void Merge(int[] nums1, int m, int[] nums2, int n) {
    
    
        int Index1=m-1;
        int Index2=n-1;
        int IndexLast=m+n-1;

        while(Index2>=0){
    
    
            if(Index1<0){
    
    
                nums1[IndexLast--]=nums2[Index2--];
            }else if(nums1[Index1]>nums2[Index2]){
    
    
                //1>2
                nums1[IndexLast--]=nums1[Index1--];
            }else{
    
    
                nums1[IndexLast--]=nums2[Index2--];
            }

        }   
    }
}

Author: guinea pig Xiaohuihui
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/122587033