您的位置:首页 > 其它

LeetCode 75 Sort Colors

2015-03-07 17:15 344 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode