leetCode(30):Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

void sortColors(vector<int>& nums)
{//排序时间复杂度O(n)
	int length=nums.size();
	int zero=0;
	int two=length-1;
	while(zero<length && nums[zero]==0)
	{//找到第一个不为0的数的下标
		zero++;
	}
	while(two>=zero && nums[two]==2)
	{//找到倒数第一个不为2的数的下标,zero前的数都是0
		two--;
	}

    for(int i=zero;i<=two;++i)
    {
    	if(nums[i]==0)
    	{//如果为0,和zero指向的数交换,zero前移
    		nums[i]=1;//zero指向的值肯定是1,首先不可能为0,其次,i遍历过的地方,2都已经换到最后
    		nums[zero]=0;
    		zero++;
    	}
    	else if(nums[i]==1)
    	{//如果为1,则跳过
    		continue;
    	}
    	else
    	{//如果为2,则和two指向的数交换,并更新two的值,
    		//和zero不同,two前面的数可能还未排序
    		nums[i]=nums[two];
    		nums[two]=2;
    		two--;
    		while(two>=i && nums[two]==2)
    		{
    			two--;
    		}
    		i--;//先向后退一步,因为two指向的数还未进行排序
    	}
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-18 07:54:33

leetCode(30):Sort Colors的相关文章

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

[LeetCode OJ] Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

【LeetCode】Sort Colors 数组排序

题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组,包含0,1,2分别代表红白蓝三种颜色,要求按照0,1,2的顺序,将同类颜色的连续排列 思路:计数排序,是一个遍历两遍的方法:可以先统计每种的数量,之后直接将这一范围内的所有值都赋值为相应的数字即可 遍历一遍的话可以在遍历的同时分别与0和2比较,从头和尾一起交换,1的在中间不用做处理: * */ package javaTr

LeetCode 75. Sort Colors(排序颜色)

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[leetcode 75] Sort Colors

1 题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, a

【LeetCode】Sort Colors 解题报告

[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, a

leetcode No75. Sort Colors

Question: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, whi

leetcode 【 Sort Colors 】python 实现

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, an

【leetcode】Sort Colors(middle)☆

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl