https://leetcode.com/problems/sort-colors/#/description Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent ...
参考链接为https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/ 思路类似于三路快排,把数组arr[0, N-1] 划分为四个区间,如下所示: a[0, i-1] zeroes a[i, k-1] ones a[k, j] unknown a[j+1, len(a)-1] twos 初始值时,i=0,j=len(a)-1,而k=0,所以z...
遍历一遍的方法:https://leetcode.com/discuss/17000/share-my-one-pass-constant-space-10-line-solution the idea is to sweep all 0s to the left and all 2s to the right, then all 1s are left in the middle. classSolution {public:voidsortColors(intA[],intn) {intsecond=n-1, zero=0;f...