[Sequence table] leetcode topic: try and die

content

foreword

Question 1: 27. Remove elements

1. Original title link

2. Topic description

3. The code function given

 4. Thought analysis

4. Code implementation

Question 2: 88. Merge two sorted arrays

1. Original title link

2. Topic description

3. The given code function

4. Analysis of ideas

4. Code implementation


foreword

The sequence table is no stranger to everyone, so let's practice the related OJ questions directly, and by the way, enhance the proficiency of direct mastery. This article records the process of my doing some questions, ok, not much to say, directly enter the theme.

Question 1: 27. Remove elements

1. Original title link

Portal: 27. Remove Elements

2. Topic description

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.

illustrate:

Why is the returned value an integer, but the output answer is an array?

Note that the input array is passed "by reference", which means that changes to the input array within the function are visible to the caller.

You can imagine the internal operation as follows:

// nums are passed "by reference". That is, without making any copies of the arguments
int len ​​= removeElement(nums, val);

// Modifications to the input array in the function are visible to the caller.
// Based on the length returned by your function, it will print out all elements in the array within that length.
for (int i = 0; i < len; i++) {     print(nums[i]); }

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2]
Explanation: The function should return a new length of 2, and the first two elements in nums are both 2. You don't need to consider elements in the array beyond the new length. For example, the new length returned by the function is 2, and nums = [2,2,3,3] or nums = [2,2,0,0] would also be considered the correct answer.
Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3]
Explanation: The function should return the new length 5, and the first five elements in nums are 0, 1, 3, 0, 4. Note that these five elements can be in any order. You don't need to consider elements in the array beyond the new length.

 

3. The code function given

 4. Thought analysis

Let's take a look. In fact, this question is essentially a question of deleting elements. The key is how do we delete them? How to delete:

Idea 1: Direct Method

Come up directly without saying a word, just traverse it directly, let the value of val be compared with the elements in the array, is it val? If yes, keep going. If not, let the following elements move forward and cover them directly until the traversal is completed. Think about it carefully, what is the time complexity of this? O(N^2) why why why? Worst case: most of the array is val, delete a data is n-1, n-2. n-3... This is an arithmetic sequence. According to the arithmetic sum formula, the time complexity is of course O(N^2).

Idea 2: space for time

How do you want to optimize it at this point? Let’s start with a simple one, how to convert the time complexity to O(N) and make it as low as possible

Many people will easily think of: open up an array, exchange space for time, put the value that is not val into the new array, and copy the value of the new array. The time complexity is O(N) and the space complexity is O(N)

 ? ? It's time to change your mind

Idea 3: Double pointer: time complexity is O(N), space complexity is O(1)

Define two pointers src and dst, both pointing to the first element at the beginning.

1. If the src position is not val, put it in the dest position, then src++, dst++

2. If the scr position is val, src++

3.dst is the new length

4. Code implementation

After talking about the idea, I probably have a basic framework in my heart. Next, we will directly implement our code.

int removeElement(int* nums, int numsSize, int val){
    int src = 0,dst = 0;
    while(src <numsSize)
    {
        if(nums[src] !=val)
        {
            nums[dst] = nums[src];
            src++;
            dst++;
        }
        else
        {
            src++;
        }
    }
    return dst;
}

 Look directly at our effect code map

 

Time beat 100%.

Question 2: 88. Merge two sorted arrays

1. Original title link

Portal: 88. Merge Two Sorted Arrays

2. Topic description

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.

3. The code function given

 

4. Analysis of ideas

 The difficulty of this question is actually very low. It is ordered in itself. It does not require us to sort it, which greatly reduces the difficulty. Besides, it is still an array, let us take a look.

This is actually like a merger, any good ideas?

Under normal circumstances, we will make a new array, and then compare and put into the new array. But this problem is not like this, requiring us to merge to nums1.

Note: orderly, then we have to make good use of this point, we can arrange from the back to the front, take the big ones and put them from the back to the front, and take the big ones and put them from the back to the front. This is the general idea of ​​​​this question.

When nums2 ends, nums1 has not ended, we do not need to deal with it, when nums1 ends, nums2 has not ended, how should we deal with it?

4. Code implementation

In this question, we only need to use m and n, which is enough.

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
  int end1 = m-1;
  int end2 = n-1;
  int end = m+n-1;
  while(end1>=0&&end2>=0){
      if(nums1[end1]>nums2[end2])
      {
          nums1[end] = nums1[end1];
          --end;
          --end1;
      }
      else{
           nums1[end] = nums2[end2];
          --end;
          --end2;
      }
  }
  while(end2>=0){
       nums1[end] = nums2[end2];
          --end;
          --end2;
  }


}

 

Well, this is the end of this time, everyone give a like and support

Guess you like

Origin blog.csdn.net/weixin_60478154/article/details/124049625