How efficient and orderly array of deduplication

Given a sorted array, the element needs to be removed in situ repeated such that each element is present only once, after returning the new length of the array is removed

Array nums = [0,0,1,1,1,2,2,3,3,4]

Function should return the new length is 5, and the first five elements of the original array is modified as 0,1,2,3,4 nums. Do not use extra space array, the input data must be modified and completed in situ under the conditions of (1) additional space O

 

analysis:

For problems. If you do not require space and time requirements, then it is very easy. But requires O (1) time. Therefore we have to find ways to solve another. This premise is an ordered array, so the same numbers are aligned together. Bits can be used where two pointers, a pointer is slow, a fast pointer. Fast pointer before slow pointer, when encountering repeated number, the pointer has moved fast forward, slow, and when the pointer does not encounter the same numerals, and the slow movement of the pointer, and a pointer value to replace this time fast slow the value of the pointer. Before the final slow pointer value is not repeated after the value are repeated.

The following example: s behalf slow pointer, F pointer fast

0      0      1      1      1      2      2      3      3      4

S      F

 

1 num [s] ==. F pointer is moved forwardly num [f]

 

0      0      1      1      1      2      2      3      3      4

S              F

 

2 num [s]! =. S pointer is moved forwardly num [f], and the num [s] = num [f]. F pointer continues to move forward

 

0      1      1      1      1      2      2      3      3      4

  S              F

 

3 num [s] ==. F pointer is moved forwardly num [f]

 

0      1      1      1      1      2      2      3      3      4

  S                      F

 

4 num [s] ==. F pointer is moved forwardly num [f]

 

0      1      1      1      1      2      2      3      3      4

  S                              F

5 num [s]! =. S pointer is moved forwardly num [f], and the num [s] = num [f]. F pointer continues to move forward

 

0      1      2      1      1      2      2      3      3      4

    S                              F

6 num [s] ==. F pointer is moved forwardly num [f]

 

 

0      1      2      1      1      2      2      3      3      4

    S                                      F

7 num [s]! =. S pointer is moved forwardly num [f], and the num [s] = num [f]. F pointer continues to move forward

 

0      1      2      3      1      2      2      3      3      4

        S                                      F

8 num [s] ==. F pointer is moved forwardly num [f]

 

0      1      2      3      1      2      2      3      3      4

        S                                              F

9 num [s]! =. S pointer is moved forwardly num [f], and the num [s] = num [f]. F pointer continues to move forward

 

At this time, before the array pointer is not repeated s array

0      1      2      3      4      2      2      3      3      4

         S          

code show as below:

int removeDuplicates(int num[], int len)
{
    int slow, fast;
    slow = 0;
    fast = 1;
    while (fast < len)
    {
        if (num[fast] != num[slow])
        {
            slow++;
            num[slow] = num[fast];
        }
        fast++;
    }
    return slow+1;
}

 

                           

Guess you like

Origin www.cnblogs.com/zhanghongfeng/p/11771758.html