Solution:
class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int first = 0;
int i = 0;
int second = n - 1;
while (i < second + 1) {
if (A[i] == 0) {
if (i == first) {
i++;
first++;
continue;
}
swap(A[first], A[i]);
first++;
} else if (A[i] == 2) {
swap(A[second], A[i]);
second--;
} else {
i++;
}
}
}
void swap(int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
};
==== Another attempt ====
class Solution {
public:
void sortColors(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
int i = left;
while (i <= right && left <= right) {
if (nums[i] == 0) {
if (nums[left] == 0) {
i++;
left++;
} else {
nums[i] = nums[left];
nums[left++] = 0;
}
} else if (nums[i] == 2) {
nums[i] = nums[right];
nums[right--] = 2;
} else {
i++;
}
}
}
};
============ Third attempt (foolish) ==========
class Solution {
public:
void sortColors(vector<int>& nums) {
int size = nums.size();
if (size == 0) {
return;
}
int start = 0, end = size - 1, iter = 0;
while (iter <= end) {
if (nums[iter] == 0) {
start++;
} else {
break;
}
iter++;
}
iter = end;
while (iter >= 0) {
if (nums[iter] == 2) {
end--;
} else {
break;
}
iter--;
}
iter = start;
while (iter <= end) {
if (nums[iter] == 2) {
nums[iter] = nums[end];
nums[end--] = 2;
while (end >= 0) {
if (nums[end] == 2) {
end--;
} else {
break;
}
}
} else if (nums[iter] == 0) {
nums[iter] = nums[start];
nums[start++] = 0;
while (start < end) {
if (nums[start] == 0) {
start++;
} else {
break;
}
}
} else {
iter++;
}
if (iter < start) {
iter = start;
}
}
return;
}
};
class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int first = 0;
int i = 0;
int second = n - 1;
while (i < second + 1) {
if (A[i] == 0) {
if (i == first) {
i++;
first++;
continue;
}
swap(A[first], A[i]);
first++;
} else if (A[i] == 2) {
swap(A[second], A[i]);
second--;
} else {
i++;
}
}
}
void swap(int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
};
==== Another attempt ====
class Solution {
public:
void sortColors(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
int i = left;
while (i <= right && left <= right) {
if (nums[i] == 0) {
if (nums[left] == 0) {
i++;
left++;
} else {
nums[i] = nums[left];
nums[left++] = 0;
}
} else if (nums[i] == 2) {
nums[i] = nums[right];
nums[right--] = 2;
} else {
i++;
}
}
}
};
============ Third attempt (foolish) ==========
class Solution {
public:
void sortColors(vector<int>& nums) {
int size = nums.size();
if (size == 0) {
return;
}
int start = 0, end = size - 1, iter = 0;
while (iter <= end) {
if (nums[iter] == 0) {
start++;
} else {
break;
}
iter++;
}
iter = end;
while (iter >= 0) {
if (nums[iter] == 2) {
end--;
} else {
break;
}
iter--;
}
iter = start;
while (iter <= end) {
if (nums[iter] == 2) {
nums[iter] = nums[end];
nums[end--] = 2;
while (end >= 0) {
if (nums[end] == 2) {
end--;
} else {
break;
}
}
} else if (nums[iter] == 0) {
nums[iter] = nums[start];
nums[start++] = 0;
while (start < end) {
if (nums[start] == 0) {
start++;
} else {
break;
}
}
} else {
iter++;
}
if (iter < start) {
iter = start;
}
}
return;
}
};
No comments:
Post a Comment