当前位置:网站首页>颜色分类Ⅱ
颜色分类Ⅱ
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--;
}
}
};
边栏推荐
- A practice of sharing applet configuration
- Lua 入门
- 10. DCN introduction
- 003、torchserve 调用LSTM模型预测
- 002. Torchserve calls the official library model
- 一切的开始,测试学妹
- Cesium 之实现房屋模型拆解
- 10、DCN 介绍
- 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
- Methodology of cloud service
猜你喜欢
QT的多线程详解(包含案例)
Pulsar consumer
MySQL service disappeared
What if the second construction fails to pass the post qualification examination? This article tells you
9、Wide&Deep简介
Web developer, web development background development
陈宏智:字节跳动自研万亿级图数据库ByteGraph及其应用与挑战
Recursion and merge sort
SQL daily exercise - niuke.com
Analysis of different dimensions of enterprise evaluators: enterprise evaluation of Gansu Power Investment Capital Management Co., Ltd
随机推荐
Industry development and research status based on 3D GIS technology
selenium实操-自动化登录
Business cloud migration strategy-6r
Google Earth engine (GEE) - gcom-c / Sgli L3 chlorophyll-a concentration (V3) data set (5000
Pulsar consumer
如何基于Ceph设计与构建一套软件定义存储系统
MIT 发现苹果 M1 中新型硬件漏洞:可不留痕迹攻破安全机制
Lucene from introduction to practice
004、torchserve 调用LR二分类预测
mysql 快速模拟千万级别数据量
Multi thread explanation of QT (including cases)
微服务十八 搭建 mysql 主从同步
【生态伙伴】深圳ISV宝德-Kunpeng-HCIA
CICA security involved in the enrollment storm of the contract School: the liquidated damages were only 3000 yuan. The lawyer said that the enterprise should compensate for reasonable expenses
8、DeepFM介绍
9. Introduction to wide & deep
机器学习服务助应用内文本语种在线和离线检测
想发自己的NFT,你要先搞清楚这6个问题
Lua 入门
数字化转型的深层逻辑 -企业数字化思考之二