Double pointer problem and sliding window problem

I haven't written it for several days, but I have been working on it for a while, and I haven't pulled it off in a day. I have written a lot of double pointer problems in these days, and the dynamic programming problem is slowly becoming more proficient. Find the state of the previous "second" and find the state transition equation. When it came to the double pointer problem, I really figured out how to solve the problem in 2 minutes, but when a question was realized, the answer was always wrong, and I did n’t know why. Later I discovered that I just roughly understood the changes in the pointer. However, it did not fall on the code, and there was an unclear pointer pointing problem in the code implementation. Below we list the questions: (This question is not the original one)

This question was made while playing the game, and my eyes were not easy to use. I started to write to maintain the relative order of the 0 elements. Hahahaha, the verification time has been wrong, I was blinded by myself, and later found that my question was wrong. Now, the double pointer problem, you must think clearly about how to move pointer 1 and pointer 2 in one case, and how to move pointer 2 and pointer 1 in the second case! This is important!

Below we list the code:

    public void moveZeroes(int[] nums) {
        if(nums.length==0) return;
        if(nums.length==1) return;
        int i=0;
    
        while (i<nums.length&&nums[i]!=0) i++;
        int j=i;
        for(int k=i;k<nums.length;k++){
            if(nums[k]!=0){
                nums[j]=nums[k];
                nums[k]=0;
                j++;
                while(nums[j]!=0){
                    j++;
                }
            }
        }
        return;
    }

At this time, I have a problem that when nums [i] is written in the front while while, the pointer overflows, && is not to pay attention to the back if the front is not established, || is to not look at the back if the front is established Yes, it must be noted that this will affect the change of variables!

Published 17 original articles · liked 0 · visits 143

Guess you like

Origin blog.csdn.net/qq_33286699/article/details/105487037