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.

就是把三种颜色相同的放在一起,0,1,2,代表三种颜色,按0,1,2排序。

算法分析:其实就是数组排序的问题,但是数组元素只有三种,所以,可以采用快速排序的思想,设置两个指针,先把0颜色放在最左边,然后把1颜色放在次左边。

也可以直接使用快速排序,只不过时间复杂度有点高,并且栈溢出。

public class SortColors
{
	public static void sortColors(int[] nums)
	{
        int start = 0;
        int end = nums.length - 1;
        while(start <= end)//把0都放在左边
        {
        	if(nums[start] != 0)
        	{
        		if(nums[end] == 0)
        		{
        			int temp = nums[start];
        			nums[start] = nums[end];
        			nums[end] = temp;
        			start ++;
        			end --;
        		}
        		else
        		{
        			end --;
        		}
        	}
        	else
        	{
        		start ++;
        	}
        }

        end = nums.length - 1;
        while(start <= end)//把1都放在左边
        {
        	if(nums[start] != 1)
        	{
        		if(nums[end] == 1)
        		{
        			int temp = nums[start];
        			nums[start] = nums[end];
        			nums[end] = temp;
        			start ++;
        			end --;
        		}
        		else
        		{
        			end --;
        		}
        	}
        	else
        	{
        		start ++;
        	}
        }
    }

	//快速排序
	public static void sortColors2(int[] nums)
	{
		quickSort(nums, 0, nums.length - 1);
	}
	public static void quickSort(int[] nums, int left, int right)
	{
		int leftIndex = left;
		int rightIndex = right;
		int pivot = nums[(leftIndex + rightIndex)/2];
		while(leftIndex <= rightIndex)
		{
			while(nums[leftIndex] < pivot)
			{
				leftIndex ++;
			}
			while(nums[rightIndex] > pivot)
			{
				rightIndex --;
			}
			if(leftIndex <= rightIndex)
			{
				int temp = nums[leftIndex];
				nums[leftIndex] = nums[rightIndex];
				nums[rightIndex] = temp;
				leftIndex ++;
				rightIndex --;
			}
			if(leftIndex < right)
			{
				quickSort(nums, leftIndex, right);
			}
			if(rightIndex > left)
			{
				quickSort(nums, left, rightIndex);
			}
		}
	}
}
时间: 2024-08-01 15:38:57

Sort Colors,颜色排序的相关文章

[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] 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 (颜色排序) 解题思路和方法

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 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,

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

[C++]LeetCode: 127 Sort Colors (计数排序 &amp; 快速排序)

题目: 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 75 Sort Colors 计数排序,三路快排

解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortColors(vector<int>& nums) { int count[3] = {0}; //存放0,1,2三个元素的频率 for(int i=0;i<nums.size();i++){ assert(nums[i] >=0 && nums[i]<=2

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

颜色排序--Sort Colors

https://leetcode.com/problems/sort-colors/ 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 integer

【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】

[075-Sort Colors (颜色排序)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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