当前位置:网站首页>颜色分类Ⅱ
颜色分类Ⅱ
2022-06-13 12:35:00 【影中人lx】
题目
方法一:分治法
算法思路:
每次选定一个中间的颜色,这个中间的颜色用给出的k来决定,将小于等于中间的颜色的就放到左边,大于中间颜色的就放到右边,然后分别再递归左右两半。
代码思路:
递归函数设置四个参数,序列需要处理区间的左右端点和处理的颜色区间
根据给定的颜色区间决定中间的颜色
将小于等于中间的颜色的就放到左边,大于中间颜色的就放到右边
递归左右两半,直到颜色区间长度等于1
class Solution {
public:
//分治法
void sortColors2(vector<int> &colors, int k) {
//如果colors数组为空或者只有1个数
//直接返回原数组
if(colors.size()<2){
return ;
}
mysort1(colors,0,colors.size()-1,1,k);
}
//start和end分别是排序范围的开始和结束,colorstart,colorend分别是需要排序颜色的大小
void mysort1(vector<int>&colors,int start,int end,int colorstart,int colorend){
int left=start,right=end;
if(left>=right||colorstart==colorend){
return ;
}
//把小于colorkey的放在左边,大于colorkey的放在右边
int colorkey=(colorend-colorstart)/2+colorstart;
while(left<=right){
//在左边区间找到一个大于colorkey的数
while(left<=right&&colors[left]<=colorkey){
left++;
}
//在右边区间找到一个小于colorkey的数
while(left<=right&&colors[right]>colorkey){
right--;
}
if(left<=right){
swap(colors[left],colors[right]);
}
}
//对左区间和右区间分别进行递归
mysort1(colors,start,right,colorstart,colorkey);
mysort1(colors,left,end,colorkey+1,colorend);
}
};
方法二:在原数组进行的计数排序
算法思路
我们用负数代表数字出现的次数,例如colors[i]=-cnt表示数字i出现了cnt次
代码思路
我们从左往右遍历colors数组
- 若colors[i] > 0且colors[colors[i]] < 0,那么colors[colors[i]] -= 1
- 若colors[i] > 0且colors[colors[i]] > 0,那么先用临时变量temp存下colors[i],将colors[colors[i]]赋值给colors[i],再将colors[temp] = -1
- 若colors[i] < 0,跳过
倒着输出每种颜色
另外注意数组下标是从0开始,为了避免n==k导致数组越界的情况,colors[i]对应的计数位为colors[colors[i] - 1]
class Solution {
public:
//基于原数组的计数排序法
void sortColors2(vector<int> &colors, int k) {
if(colors.size()<2){
return ;
}
mysort2(colors,k);
}
void mysort2(vector<int>&colors,int k){
int index=0;
while(index<colors.size()){
//如果index已经是计数位,就直接跳过
if(colors[index]<=0){
index++;
}
//如果index不是计数位
else{
//因为数组的下标是0开始的,所以映射需要减1
//比如数值为1的个数存放在colors[0]的位置
//数值为2的个数存放在colors[1]的位置
int tmp=colors[index]-1;
//如果colors[tmp]是计数位
if(colors[tmp]<=0){
colors[tmp]--;
//因为colors[index]被计数了,所以index这个位置被置为0
colors[index]=0;
index++;
}
//如果两个都不是计数位
//由于是对index进行计数,所以需要保存colors[tmp]的数值
//然后把colors[index]位置作为是计数位
else{
swap(colors[tmp],colors[index]);
colors[tmp]=-1;
}
}
}
//倒着打印数组
int num=colors.size()-1;
//值为k的个数存放在数组下标为k-1的位置
while(k>0){
for(int j=0;j>colors[k-1];j--){
colors[num--]=k;
}
k--;
}
}
};
边栏推荐
- Build a secure cloud platform
- [MySQL lock table processing]
- algorithm:派对的最大快乐值
- Pointnet: deep learning on point sets for 3D classification and segmentation
- That is to say, it launched the industry's first data stream recording PAAS scheme, which can reproduce the recording capacity of large manufacturers at low cost
- 亚信安全陷解约校招生风波:违约金仅3000元 律师称企业需赔偿合理费用
- Recursion and merge sort
- [management knowledge] risk of "risk register"
- CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结
- Interpretation of cube technology | past and present life of cube Rendering Design
猜你喜欢
A collection of excellent learning resources of PostgreSQL (including basic manuals, practical skills & cases, and book recommendations)
Step by step introduction to sqlsugar based development framework (7) -- in the file upload module, the option mode [options] is used to handle routine upload and FTP file upload
Pointnet: deep learning on point sets for 3D classification and segmentation
Interpretation of cube technology | past and present life of cube Rendering Design
IPv6 connection test passed, but failed to Ping. Problem solved (record)
Lucene from introduction to practice
Rsync downlink synchronization, rsync+inotify real-time synchronization
CentOS环境下安装KingbaseES数据库
Multi thread explanation of QT (including cases)
Branch prediction of CPU
随机推荐
IPv6 connection test passed, but failed to Ping. Problem solved (record)
即构推出行业首个数据流录制PaaS方案,低成本复刻头部大厂录制能力
5 locksupport and thread interruption
数字化转型思考系列之一 -- 企业信息化
Opencv learning notes (II): reading MNIST datasets
想发自己的NFT,你要先搞清楚这6个问题
mysql中max_connections与max_user_connections使用区别
9、Wide&Deep简介
How to quickly SAA traditional applications at the minimum cost
I want to change my career as a programmer. Can I find a job after a programming training course? Can I teach myself?
11. PCA introduction
(转载)Multi-Context CoreData 之多线程环境中使用CoreData
MySQL service disappeared
Branch prediction of CPU
等个有“源”人|OpenHarmony 成长计划学生挑战赛报名启动
Google Earth Engine(GEE)——计算ndvi的零星植被状况(墨西哥为例)
Rotating hourglass in loading
实战模拟│企业微信机器人实时报错预警
Options request (cross domain pre check)
Methodology of cloud service