[Leetcode] Detailed array of daily selected questions: what? Want to remove my element?

        Hi, everyone, I’m Chef Yuan (because I love cooking, I got my chef certificate). I have been reading blogs written by everyone before and learned a lot. Then recently I have developed an idea I wrote, and I will share what I know with the students in need. In the future, I will share various solutions of Leetcode's selected topics and some small Demos of Python, JS, JQ, CSS, PHP, JAVA every day. Please pay attention to me and exchange and study together.


     My classmates, I recently had a long-term plan to sort out the leetcode topics, from simple to deep, from simple to complex. It is a grand project, so I plan to sort out one or two classic topics every day to complete this one. Process I believe we will definitely gain a lot. If you want to team up with buddies, we can check in the group together and make progress together. You can add me on WeChat tan45du_one or scan the QR code in the summary.

Title description

Insert picture description here


Violent solution

Insert picture description here

Question ideas

This problem can be regarded as a simple problem, suitable for novices, and then everyone should not look down on the violent solution, we can write the violent solution first, and then think about other methods, which is of great help to our coding ability. Let's analyze the idea of ​​this question. His meaning is to let us delete the elements in the array, and then follow the elements behind the array. Finally, return the length of the array with the deleted elements. For example, the length of the array is 10, and there are 2 target values ​​in it. The length we finally return
is 8, but the 8 elements returned need to be at the top of the array. Then the brute force solution requires two for loops, one to find and delete, and the other to update the array.
Insert picture description here
Insert picture description here
The overall idea is this, and the latter will continue to be covered. The brute force solution does not time out, and the implementation is not too simple. There are two main points to pay attention to.
(1) You need to define the variable len to get the length of the array. Because the length of the returned array will change later, nums cannot be used. Length as the upper bound
(2) Whenever we find a value that needs to be deleted, we need i-- to prevent the occurrence of multiple values ​​that need to be deleted together, and then omit deletion.

Topic code

The code is also relatively simple

class Solution {
    
    
    public int removeElement(int[] nums, int val) {
    
    
        //特殊情况需要注意
        if(nums.length == 0){
    
    
            return 0;
        }
        //获取数组长度,作为for循环的上界
        int len = nums.length;
        for(int i = 0; i < len ; i++){
    
    
            //找到需要删除的元素
            if(nums[i]==val){
    
     
               //覆盖需要删除的元素               
                for(int j = i+1 ; j < len ; j++){
    
    
                    nums[j-1] = nums[j];
                }
                //保留当前索引,防止漏删
                i--;
                //缩小需要返回的长度
                len--;                
            }
        }
        return len;
    }
}

Speed ​​pointer

Insert picture description here

Question ideas

The method of fast and slow pointers is more interesting. It only needs a for loop to solve the problem. The time complexity is O(n). The general idea is to have two pointers. The first one is followed by the other. The previous one is used to search for the value to be deleted. When the value that needs to be deleted is reached, the front pointer skips directly, and the back pointer does not move. When a normal value is encountered, both pointers move and modify the value of the slow pointer. Finally, just output the index of the slow pointer.
Insert picture description here

Topic code

The code is relatively simple, you can refer to it

class Solution {
    
    
    public int removeElement(int[] nums, int val) {
    
    
    //特殊情况
      if(nums==null){
    
    
          return 0;
      }     
      int j = 0;//慢指针,i代表快指针
      for(int i = 0;i<nums.length;i++){
    
    
         //正常情况直接赋值给i          
          if(nums[i]!=val){
    
    
              nums[j]=nums[i];
              j++;
          }
          //如果为需要删除的值时,则快指针移动,慢指针不动。
      }
       return j;
    }
}

to sum up

Generally speaking, this question is pretty good. It can be regarded as opening the door to dual pointers. There will be many dual pointers in the future. Please add my friends to the questions.
![Insert picture description here](https://img-blog.csdnimg.cn/20201013143801804.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNQ1g4femVh16,color=ZHFFbG9nLmNQ1g4femVh16

Author: LeetCode
link: https: //leetcode-cn.com/problems/rotate-array/solution/xuan-zhuan-shu-zu-by-leetcode/
Source: stay button (LeetCode)
copyright reserved by the authors. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/tan45du_yuan/article/details/109046609