[leetcode/Matrix](sort)Sort Colors-计数排序的运用 约定不等于承诺〃 2021-09-28 07:46 350阅读 0赞 ### Sort Colors 计数排序 ### * * 题干: * 第一种解法:计数排序 * 第二种解法:头尾指针解法 ## 题干: ## Given an array with n objects colored red, white or blue, sort them in-place(the input is overwritten by the output as the algorithm executes) 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. Note: You are not suppose to use the library’s sort function for this problem. Example: Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2] Please submit a cpp file with the implementation of function`void sortColors(vector<int>& nums)`, you can start like this: void Solution::sortColors(vector<int>& nums){} Follow up: A rather straight forward solution is a two-pass algorithm using counting sort. First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s. Could you come up with a one-pass algorithm using only constant space? ## 第一种解法:计数排序 ## ① 扫描第一遍,找出最大最小数 ② 开辟一个数组a,长度为max-size+1 ③ 扫描第二遍数组,记录每个数字出现的频率即a\[nums\[i\]\]++ ④ 扫描数组a,输出 适用于已经知道最大最小的情况,这是一种牺牲空间的做法 #include"Solution.h" #include<algorithm> #include<string.h> void Solution::sortColors(vector<int>& nums){ int size=nums.size(); int min=1e10; int max=0; for(int i=0;i<size;i++){ if(nums[i]<min){ min=nums[i]; } if(nums[i]>max){ max=nums[i]; } } //遍历一遍找出最大最小值 int *a=new int[max-min+1]; memset(a,0,(max-min+1)*sizeof(int)); for(int i=0;i<size;i++){ a[nums[i]-min]++; } int count=0; for(int i=0;i<max-min+1;i++){ while(a[i]){ a[i]--; nums[count++]=i+min; } } delete []a; } ## 第二种解法:头尾指针解法 ## left左边全是0,right右边全是2 中间是1 #include"Solution.h" void Solution::sortColors(vector<int>& nums){ int left=0; int right=nums.size()-1; int i=0; while(i<=right){ if(nums[i]==0){ swap(nums[left],nums[i]); left++; i++; } else if(nums[i]==2){ swap(nums[right],nums[i]); right--; } else i++; } } void swap(int &a,int& b){ int temp=a; a=b; b=temp; }
相关 计数排序 数排序是非比较排序,桶排序的一种 时间复杂度:O(n),空间复杂度O(n) ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_1... 一时失言乱红尘/ 2024年04月18日 08:20/ 0 赞/ 151 阅读
相关 计数排序 计数排序 冒泡、选择、插入、归并、快速、希尔、堆排序,都是基于比较的排序,平均时间复杂度最低是O(nlogn)。 计数排序、桶排序、基数排序,都不是基于比较的排序,它们 痛定思痛。/ 2023年02月20日 10:43/ 0 赞/ 58 阅读
相关 【排序】计数排序 为什么记录这个 在做 [leetcode 561 题][leetcode 561]的时候,第三种解法,看了很久才明白,后来理解实际上是做了一次计数排序。 这个题,直接看 缺乏、安全感/ 2022年11月20日 02:00/ 0 赞/ 299 阅读
相关 计数排序 转载自:[http://www.cnblogs.com/eaglet/archive/2010/09/16/1828016.html][http_www.cnblogs.com Bertha 。/ 2022年08月11日 08:00/ 0 赞/ 283 阅读
相关 计数排序 计数排序,他的主要目的是对整数排序并且会比普通的排序算法性能更好。 1. 初始化一个计数数组,大小是输入数组中的最大的数。 2. 遍历输入数组,遇到一个数 布满荆棘的人生/ 2022年07月14日 01:26/ 0 赞/ 317 阅读
相关 计数排序 对于一个int数组,请编写一个计数排序算法,对数组元素排序。 给定一个int数组A及数组的大小n,请返回排序后的数组。 测试样例: [1,2,3,5,2, 系统管理员/ 2022年06月09日 01:36/ 0 赞/ 308 阅读
相关 计数排序 计数排序 算法描述:是一种通过计数来达到排序的方法。 ![20180326152550965][] 1.选出数组的最大值k,创建一个k+1长度的数组coun 素颜马尾好姑娘i/ 2022年05月28日 00:20/ 0 赞/ 329 阅读
相关 计数排序 / 计数排序:统计小于等于该元素值的元素的个数i,于是该元素就放在目标数组的索引i位(i≥0)。 计数排序基于一个假设,待排序数列的 ゝ一纸荒年。/ 2021年12月18日 05:07/ 0 赞/ 399 阅读
相关 [leetcode/Matrix](sort)Sort Colors-计数排序的运用 Sort Colors 计数排序 题干: 第一种解法:计数排序 第二种解法:头尾指针解法 题干: Given an arr 约定不等于承诺〃/ 2021年09月28日 07:46/ 0 赞/ 351 阅读
相关 计数排序 计数排序: 假设n个输入元素中的每一个都是在0~区间内的一个整数,其中k为某个整数。当k=O(n)是,排序时间为O(n). 基本思想:对每个输入元素x,确定小于x元素的 淡淡的烟草味﹌/ 2021年09月23日 03:22/ 0 赞/ 517 阅读
还没有评论,来说两句吧...