void swap(int *a, int *b) { int t = *a; *a = *b, *b = t; } void sortColors(int *nums, int numsSize) { int p0 = 0, p1 = 0; for (int i = 0; i < numsSize; ++i) { if (nums[i] == 1) { swap(&nums[i], &nums[p1]); ++p1; } else if (nums[i] == 0) { swap(&nums[i], &nums[p0]); if (p0 < p1) { swap(&nums[i], &nums[p1]); } ++p0; ++p1; } } }
void swap(int *a, int *b) { int t = *a; *a = *b, *b = t; } void sortColors(int *nums, int numsSize) { int p0 = 0, p1 = 0; for (int i = 0; i < numsSize; ++i) { if (nums[i] == 1) { swap(&nums[i], &nums[p1]); ++p1; } else if (nums[i] == 0) { swap(&nums[i], &nums[p0]); if (p0 < p1) { swap(&nums[i], &nums[p1]); } ++p0; ++p1; } } }