Nine chapters count judges Net-original website
http://www.jiuzhang.com/problem/70/
Topic
To an array, and the value of the elements inside the array is only likely to be 0,1,2, and then the array is now sorted.
Online test
http://www.lintcode.com/en/problem/sort-colors/
Answer
The method of one pass requires two pointer techniques. With two pointers, a pointer x pointing to the array header is the position of 0 and 1, the other pointer y points to the position where the tail represents the difference between 1 and 2, and then we iterate over the array, and if the element currently encountered is 0, then we swap it with the element where x points to the position, and X moves forward, updating the boundary; Then, if it is 2, then replace the element where Y points to, and y back one step, so that X is the position of 0 and 1, and y is the position of 1 and 2. This method of time complexity O (n), Spatial complexity O (1)
Nine-chapter algorithm surface question 69 row color sort Colors