[The data is in order, and the same element retains k bits]

Leetcode has read the explanation of Sanye, and then summed it up. This method is suitable for data sorting and orderly, and it is required to keep only a few of the same elements in it, such as:

80. Remove Duplicates in Sorted Array II

 

analyze:

Because it is required to keep at most 2 identical elements for each element, you can directly keep the first two elements first , and then compare the following elements. If the third element is still the same as the second element, then go back and continue Judgment until it is different from the first two elements , then assign the third element to this value, and so on.

Such as: [0,1,1,1,2,2,2,2,3,4,5,5]

First use a variable to record the insertion position, int idx=0;

1. idx = 0, at this time the array is: []

2. Reserve the first two digits, the array is: [0,1]

3. Then judge whether the third element is equal to the second element (1), if it is equal, skip it until the unequal (1) is reserved, as shown in the example, the third element is 1, skip it until If the fifth bit is 2 and not equal to 1, it is reserved.

Then the judgment at this stage needs to meet the two blackened parts of the above analysis.

①, first keep the first two elements

Here, because the requirement is 2 bits, a variable k = 2 is defined here to limit each element to appear k (2) times , so it is necessary to limit idx, because the initial value of idx is set to 0, so condition 1 : idx < k. After retaining the first two elements, idx=2 at this time

②, until it is different from the previous two elements

Since it is an ordered element, the following element must be compared with the previous element first, that is,  compared with the previous (2) elements at the current position k , if it is different, it will be kept . For example, in this example, the third element is 1, then 1 To compare with the first element 0, if it is different, then keep it, because at this time idx = 2, to keep the third element, then nums[idx++] = x; then the fourth element 1, compare with the second element 1 , the same is not reserved, at this time idx = 3, then the fifth element 2, compared with the third element 1, if different, then reserved, at this time idx = 3, to retain the fourth element, then nums[idx ++ ] = x. By analogy, each element can be guaranteed to appear at most 2 times. So the second condition is: nums[idx -k] != x

Since the above two conditions are successive and not all must be established at the same time, the condition uses || 

From this, write the code as follows:

for(int x:nums){
    if(idx <=k || nums[idx -k] != x) nums[idx++] = x;
}

Ultimately it can be written as follows:

class Solution {
    public int removeDuplicates(int[] nums) {   
        return pross(nums,2);
    }

    int pross(int[] nums,int k){ //k为重复元素最多保留几位,此题k=2
        int idx =0;
        for(int x: nums){
            if(idx <k || nums[idx -k] !=x){
                nums[idx++] = x;
        }
    }
        return idx;
  }
}

 

Guess you like

Origin blog.csdn.net/Sean_Asu/article/details/125269532