/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
const length=nums.length-1;
let strat=0;
let end=length;
let cur=0;
let cval;
function swap(start,end){
const temp=nums[start];
nums [start]=nums[end];
nums[end]=temp;
}
while(cur<=end){
cval=nums[cur];
if(cval===0){
swap(cur,strat);
strat++;
cur++;
}
if(cval===2){
swap(cur,end);
end--;
}
if(cval===1){
cur++;
}
}
};
【leetcode】颜色分类双指针
最新推荐文章于 2024-08-29 22:36:09 发布