当前位置:网站首页>颜色分类Ⅱ
颜色分类Ⅱ
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--;
}
}
};
边栏推荐
- MIT found a new hardware vulnerability in Apple M1: it can break the security mechanism without leaving a trace
- Intelligent customer service system framework rasa
- Principle and specific operation process of hyperspectral true color image synthesis
- 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
- The answer to the subject "highway" of the second construction company in 2022 has been provided. Please keep it
- QT的多线程详解(包含案例)
- 机器学习服务助应用内文本语种在线和离线检测
- 基于SqlSugar的开发框架循序渐进介绍(7)-- 在文件上传模块中采用选项模式【Options】处理常规上传和FTP文件上传
- 微服务十八 搭建 mysql 主从同步
- Cvpr2022 | a convnet for the 2020s & how to design neural network Summary
猜你喜欢

机器学习服务助应用内文本语种在线和离线检测

Interview shock 56: what is the difference between clustered index and non clustered index?

options请求(跨域预检)

Cesium 之实现房屋模型拆解

MIT found a new hardware vulnerability in Apple M1: it can break the security mechanism without leaving a trace

Pointnet: deep learning on point sets for 3D classification and segmentation

CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结

Opencv learning notes (II): reading MNIST datasets

(转载)Multi-Context CoreData 之多线程环境中使用CoreData

Pulsar consumer
随机推荐
LVGL库入门教程01-移植到STM32(触摸屏)
Rsync downlink synchronization, rsync+inotify real-time synchronization
002、torchserve调用官方库模型
SQL日常练习-牛客网
Camunda timer events example demo (timer events)
婴儿EEG数据的多元模式分析(MVPA):一个实用教程
Actual combat simulation │ real time error alarm of enterprise wechat robot
10、DCN 介绍
7、场感知分解机FFM介绍
PostgreSQL精品学习资源合集(含基础手册、实操技巧&案例、书籍推荐)
Selenium3自动化测试实战(5)
QT的多线程详解(包含案例)
Multi thread explanation of QT (including cases)
[MySQL lock table processing]
蜜月期过后,跨境电商的出口在哪里?亚马逊云科技全新洞察发布
This article clearly explains the design of authority management in tob SaaS system
Interview shock 56: what is the difference between clustered index and non clustered index?
The answer to the subject of Municipal Administration of the second construction company in 2022 has been provided. Please keep it
9、Wide&Deep简介
Branch prediction of CPU