题目链接:https://www.lintcode.com/problem/sort-colors/description
/**
* @param nums: A list of integer which is 0, 1 or 2
* @return: nothing
*/
func sortColors (nums *[]int) {
// write your code here
if (nums == nil || len(*nums) <= 1) {
return;
}
var pl int = 0;
var pr int = len(*nums) - 1;
var i int = 0;
for {
if (i > pr) {
break;
}
if ((*nums)[i] == 0) {
swap(nums, pl, i);
pl++;
i++;
} else if ((*nums)[i] == 1) {
i++;
} else {
swap(nums, pr, i);
pr--;
}
}
}
func swap(nums *[]int, a int, b int) {
var temp int = (*nums)[a];
(*nums)[a] = (*nums)[b];
(*nums)[b] = temp;
}