红蓝球排序,使用两个指针,当前数为0,在前一个指针上加,当前数为2,在后一个指针上减,并交换,faster than 100%
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
var l = 0
var r = nums.length - 1
var temp
for(var i = 0; i <= r; i++){
if(nums[i] === 0){
temp = nums[l]
nums[l] = 0
nums[i] = temp
l++
}else if(nums[i] === 2){
temp = nums[r]
nums[r] = 2
nums[i] = temp
i--
r--
}
}
return nums
};