OJ question: remove elements, merge two sorted arrays

Dear martial arts warriors, what did Xiao Azi bring to you today? ? ?

Two OJ questions! ! !

1. Remove elements in place: https://leetcode-cn.com/problems/remove-element/

2. Merge two sorted arrays: https://leetcode-cn.com/problems/merge-sorted-array/

Note: This time we learn OJ questions and master the following methods to realize the optimal algorithm.

Ok Ok, let's enter our first OJ question!

1. Remove elements in place

Given an array nums and a value val, you need to remove all elements whose value is equal to val in place, and return the new length of the removed array. Instead of using extra array space, you have to use only O(1) extra space and modify the input array in place. The order of elements can be changed. You don't need to consider elements in the array beyond the new length.

Suppose the current values ​​of nums and val are:

 Idea 1: Traverse one by one, when it is found to be val, move the following elements forward one by one, overwriting the value of val.

(But the idea of ​​a time complexity is O(n^2))

Idea 2: Exchange space for time. First, we put the value that is not val in the dynamically created array space, then assign the value of the dynamically created array space to nums, and finally release the dynamically created array space. (Although the title clearly requires not to use additional array space, we still have to master this method) Time complexity: O(n), space complexity O(n)

 Idea 3: Double pointer method, you can first define two pointers src and dst, if the src position is not val, put it in the dst position, then src++, dst++. The src location is val, src++. Time Complexity: O(n), Space Complexity: O(1)

 Through the analysis of the above three algorithms, we can find that the third idea is the optimal algorithm

Then let's work together to realize the third idea.

 Come on, let's analyze this algorithm for you martial arts warriors! ! !

Analysis: First, pass the address of the first element of the array, the removed value and the total number of elements, and pass them to nums, val, and sz respectively. Two integer variables src and dst are defined in my_remove, and these two variables are As the subscript of the array, use the loop to determine if nums[src] is not val, then assign the value of nums[src] to nums[dst], otherwise nums[src] continues to search until it is not val. Assignment (Note: It can also be achieved by using src and dst as pointers to search, and this is left to all chivalrous friends)


 Ok Ok, let's move on to our second OJ question!

2. Merge two sorted arrays

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

Please merge nums2 into nums1 so that the merged arrays are also arranged in non-decreasing order.

Note: Ultimately, the merged array should not be returned by the function, but stored in the array nums1. To cope with this situation, the initial length of nums1 is m + n, where the first m elements represent the elements that should be merged, and the last n elements are 0 and should be ignored. The length of nums2 is n.

Assuming nums1 and nums2, the values ​​of m and n are:

Idea 1: Compare the two arrays, and put the small one in the empty array (although the title requires it to be stored in nums1, we still have to understand this idea)

 Idea 2: Both nums1 and nums2 go from the back to the front, and the larger ones are placed from the back to the front.

  Through the analysis of the above two algorithms, we can find that the second idea is the optimal algorithm

 So let's work together to realize the second idea.

 Note: The implementation of this code is the algorithm of the second idea, you can look at the picture! ! !

QQ: 2186529582
Welcome to add my own QQ, if there is something you don't understand, let's learn together! ! !

 come on! ! !

Guess you like

Origin blog.csdn.net/m0_66488562/article/details/123399174