当前位置:网站首页>Fast and slow pointer series

Fast and slow pointer series

2022-06-24 08:51:00 accumulate steadily ض

27. Remove elements

  Double finger needling

  • Use Speed pointer
  • The fast pointer is responsible for finding the elements of our new array ( Also is val The elements of )
  • The slow pointer is responsible for maintaining this new array
class Solution {
    public int removeElement(int[] nums, int val) {
         int fast = 0;// The fast pointer is responsible for finding the elements of our new array ( Also is val The elements of )
         int slow = 0;// The slow pointer is responsible for maintaining this new array 
         for(fast =0;fast<nums.length;++fast){
             if(nums[fast]!=val){// If it is found that the value pointed by the fast pointer is not val, Then you need to assign this value to the location to be updated , After the assignment slow Go back and continue to point to the location to be updated to maintain the new array 
                 nums[slow++] = nums[fast];
             }
         }
         return slow;
    }
}

Violent solution :

  • Use two for Loop through , The outer loop iterates through each element of the array , Inner loops are used to move elements ( To cover )
  • If the array element is val That requires an inner loop to move elements
class Solution {
    public int removeElement(int[] nums, int val) {
        int length = nums.length;
        for(int i=0;i<length;++i){
            if(nums[i]==val){
                for(int j =i+1;j<length;++j){
                    nums[j-1]= nums[j];
                }
                i--;
                length--;
            }
        }
        return length;
    }
}

26. Remove duplicate items from an ordered array

  • Use Speed pointer
  • fast To find the elements of a new array , That is, the element that does not repeat the previous one
  • slow Used to maintain new arrays
class Solution {
    public int removeDuplicates(int[] nums) {
        // Use the speed pointer 
        // still fast To find the elements of a new array , That is, the element that does not repeat the previous one 
        //slow Used to maintain new arrays 
        int fast =0;
        int slow =0;
        for(fast =1;fast<nums.length;++fast){
            int tmp = nums[fast-1];// Record fast The elements in front 
            if(nums[fast]!=tmp){
                // Different from the elements of the record , Proof of need to join slow In the new array maintained 
                nums[++slow] = nums[fast];
            }
        } 
        return slow+1;
    }
}

283. Move zero

  • Use Speed pointer
  • fast To find the elements of a new array , That is, the element that does not repeat the previous one
  • slow Used to maintain new arrays
  • Last slow The new array previously maintained for us , The back is all set 0
class Solution {
    public void moveZeroes(int[] nums) {
         int fast =0;
         int slow =0;
         for(fast =0;fast<nums.length;++fast){
             if(nums[fast]!=0){
                 nums[slow++] = nums[fast];
             }
         }
         for(int i = slow;i<nums.length;++i){
             nums[i] = 0;
         }
    }
}

原网站

版权声明
本文为[accumulate steadily ض]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240637108662.html