Leetcode first _ Sort Colors

Source: Internet
Author: User

An interesting question, which I did not summarize before, uses the third pointer when modifying the array.

If it is two colors, we will certainly do it, directly one at one end and two pointers, scanning to do not belong to their own kind of swap. How can we solve this problem with a third party? Think about how to modify the linear time in an array. You must replace the current element with the one-time or constant to the location where it should finally be, or it will be complicated. So should the current element stay there? If the value is 0, it should be somewhere in front of the array where the value is 0. If the value is 2, it should be somewhere behind the array where the value is 2. If it is 1, it should be in the middle, but 0 and 2 are finally known by God, which is not well defined in the middle. However, we can know the beginning and the end, and this is the same throughout the world, so we can only give up the third party, only consider the location of the two original configurations.

The original configuration must be saved by two pointers. one pointer is at the beginning and the other is at the end. Only when new similar pointers are added will they move. Therefore, the task of scanning the array for this question must be handed over to the third pointer, which is also the key to this question. Starting from scratch, if the third pointer encounters 0, it should be replaced with the array header, and the pointer of the source 0 should be pushed back. If 2 is encountered, we should replace it with the end of the array and push forward the pointer of the original configuration 2 to a position. Here, we should note that what if it is replaced with 0? Only secondary replacement can be performed, because once this opportunity is missed, this 0 will remain in the third position. So, just like starting with 0, replace it with another one. Note that the replacement of the third pointer to the position must be a small three, because if it is the original configuration of No. 2, it has long been replaced back to the end of the array, and the third pointer is scanned in turn. Finally, if we encounter s3.1. then we will ignore it directly, because if he is in a position that should not be waiting, one day he will replace it with the original configuration, just like in life.

class Solution {public:    void sortColors(int A[], int n) {        int beg = 0, end = n-1;        for(int i=0;i<=end;i++){            if(A[i] == 0){                if(A[i] != A[beg])                    swap(A[beg], A[i]);                ++beg;            }else if(A[i] == 2){                while(end>i&&A[end] == 2)                    --end;                if(end<=i)  break;                swap(A[end], A[i]);                --end;                if(A[i] == 0){                    swap(A[beg], A[i]);                    beg++;                }            }        }    }};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.