public class Solution {
public void sortColors(int[] A) {
if (A == null || A.length == 0) return;
int zero = 0;
int two = A.length - 1;
int i = 0;
while (i <= two) {
if (A[i] == 0) {
swap(A, zero, i);
zero++;
}
if (A[i] == 2) {
swap(A, i, two);
two--;
} else {
i++;
}
}
}
private void swap(int[] A, int a, int b) {
int temp = A[a];
A[a] = A[b];
A[b] = temp;
}
}
作者:ywheel
本文出处:http://blog.ywheel.com/post/2015/03/07/leetcode_75/
文章版权归本人所有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。