当前位置:网站首页>【LeetCode-75】 颜色分类
【LeetCode-75】 颜色分类
2022-08-11 05:30:00 【Ring*】
6.14 颜色分类【75】
6.14.1 题目描述
给定一个包含红色、白色和蓝色、共 n 个元素的数组 nums ,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
必须在不使用库的sort函数的情况下解决这个问题。
6.14.2 方法一:单指针
思路与算法

class Solution {
public void sortColors(int[] nums) {
int n = nums.length;
int ptr = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
int temp = nums[i];
nums[i] = nums[ptr];
nums[ptr] = temp;
++ptr;
}
}
for (int i = ptr; i < n; ++i) {
if (nums[i] == 1) {
int temp = nums[i];
nums[i] = nums[ptr];
nums[ptr] = temp;
++ptr;
}
}
}
}
复杂度分析
- 时间复杂度:O(n),其中 n 是数组 nums 的长度。
- 空间复杂度:O(1)。
6.14.3 方法二:双指针
思路与算法
class Solution {
public void sortColors(int[] nums) {
int n = nums.length;
int p0 = 0, p1 = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] == 1) {
int temp = nums[i];
nums[i] = nums[p1];
nums[p1] = temp;
++p1;
} else if (nums[i] == 0) {
int temp = nums[i];
nums[i] = nums[p0];
nums[p0] = temp;
if (p0 < p1) {
temp = nums[i];
nums[i] = nums[p1];
nums[p1] = temp;
}
++p0;
++p1;
}
}
}
}
复杂度分析
- 时间复杂度:O(n),其中 n 是数组 nums 的长度。
- 空间复杂度:O(1)。
6.14.4 方法三:双指针
思路与算法
class Solution {
public void sortColors(int[] nums) {
int n = nums.length;
int p0 = 0, p2 = n - 1;
for (int i = 0; i <= p2; ++i) {
while (i <= p2 && nums[i] == 2) {
int temp = nums[i];
nums[i] = nums[p2];
nums[p2] = temp;
--p2;
}
if (nums[i] == 0) {
int temp = nums[i];
nums[i] = nums[p0];
nums[p0] = temp;
++p0;
}
}
}
}
复杂度分析
- 时间复杂度:O(n),其中 n 是数组 nums 的长度。
- 空间复杂度:O(1)。
6.14.5 my answer—统计0 1 2的个数
class Solution {
public void sortColors(int[] nums) {
List<Integer> list0 = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
for (int num : nums) {
if(num==0)list0.add(num);
if(num==1)list1.add(num);
if(num==2)list2.add(num);
}
for (int i = 0; i < nums.length; i++) {
if (i<list0.size())nums[i]=0;
else if(i<list0.size()+list1.size())nums[i]=1;
else nums[i]=2;
}
}
}
边栏推荐
- vim 编辑器使用学习
- helm安装
- C语言-7月22日- NULL和nullptr的深入了解以及VScode对nullptr语句报错问题的解决
- Fourth Paradigm OpenMLDB optimization innovation paper was accepted by VLDB, the top international database association
- 编译异常解决
- [Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
- mongoose连接mongodb不错,显示encoding没有定义
- C语言-7月31日-指针的总结以及typedef关键字
- 自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析
- 【无标题】
猜你喜欢
随机推荐
函数使用记录
swagger常用注释API @ApiModel、@ApiModelProperty的用法
2021-09-11 C language variables and memory allocation
Byte (byte) and bit (bit)
解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
Interpretation of the paper: Cross-Modality Fusion Transformer for Multispectral Object Detection
stack stack
nepctf Nyan Cat 彩虹猫
C语言-6月8日-给定一个字符数组‘i am a student’ 统计字符a的个数并进行输出
OpenMLDB:线上线下一致的生产级特征计算平台
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
本地服务配置内网穿透实现微信公众号整合
Day 87
精彩联动 | OpenMLDB Pulsar Connector原理和实操
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
js learning advanced (event senior pink teacher teaching notes)
微信小程序_开发工具的安装
USB URB
He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone









