75.Sort Colors(法1快排法2线性扫描统计赋值)

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 inthe order red, white and blue.

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

Note:

You are not suppose to use the library‘s sort function for this problem.

click to show follow up.

HideTags

Array Two
Pointers
 Sort

#pragma once
#include<iostream>
#include<vector>
using namespace std;

//法1:参数为 数组和数组大小 的快速排序
void sortColors(int A[], int n)
{
	if (n <= 1)
	{
		cout << "return" << endl;
		return;
	}
	int pivot = A[0];
	int begain = 0;
	int end = n - 1;
	while (begain < end)
	{
		while (A[end] >= pivot && end > begain)
			end--;
		A[begain] = A[end];
		while (A[begain] <= pivot&&begain < end)
			begain++;
		A[end] = A[begain];
	}
		A[begain] = pivot;
		sortColors(A, begain);
		sortColors(A + begain + 1, n - begain - 1);
}

//法2:线性扫描,统计次数,重新赋值
void sortColors2(int A[], int n)
{
	int colors[] = { 0, 0, 0 };
	for (int i = 0; i < n; i++)
		colors[A[i]]++;
	int index = 0;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < colors[i]; j++)
			A[index++] = i;
}

void main()
{
		int A[] = { 1, 2, 2, 2, 2, 0, 0, 0, 1, 1 };
	sortColors2(A, 10);
	for (int i = 0; i < 10; i++)
		cout << A[i] << ' ';
	cout << endl;
	system("pause");
}
时间: 2024-08-09 02:04:41

75.Sort Colors(法1快排法2线性扫描统计赋值)的相关文章

最简单快速的排序法之桶排法

前提:0-100内的随机数N个,实现从小到大(从大到小)排序. 实现:新建一个长度为101的数组,value初始化为0.数组每个key代表0-100中的数字,value值表示0-100中任意一个数组的出现次数. 通俗点说就是每个key代表一个桶,我们有101个桶,每个桶上表上数字0-100.把要排序的数字扔到对应的桶里,桶里扔一个数字时相应的key的value值就+1,表示桶里有几个数字. 代码实现: $numbers = array(63,6,98,54,88,5,89,16,59,10,31

No.75 Sort Colors

No.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

刷题75. Sort Colors

一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是简单的,代码如下: class Solution{ public: void sortColors(vector<int>& nums){ int num0=0,num1=0,num2=0; for(int i=0;i<nums.size();i++){ if(nums[i]==0)

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]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 OJ 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 颜色排序

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

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