当前位置:网站首页>【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;
}
}
}
边栏推荐
- C语言实现三子棋(代码详解)
- 本地缓存cookie的使用
- Tinker接入全流程---编译篇
- 构建面向特征工程的数据生态 ——拥抱开源生态,OpenMLDB全面打通MLOps生态工具链
- 星盟-pwn-babyfmt
- 第一章 Verilog语言和Vivado初步使用
- The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
- Tinker接入全流程---配置篇
- Real-time Feature Computing Platform Architecture Methodology and Practice Based on OpenMLDB
- Tinker's self-introduction
猜你喜欢
连接数据库时出现WARN: Establishing SSL connection without server‘s identity verification is not recommended.
JS advanced web page special effects (pink teacher notes)
自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析
Jetpack之dataBinding
2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
开发公众号授权遇到的redirect_uri参数错误
mk file introduction
Getting Started with JNI
C语言-6月8日-给定一个字符数组‘i am a student’ 统计字符a的个数并进行输出
Manufacturer Push Platform-Huawei Access
随机推荐
PAT乙级刷题之路
C语言实现扫雷游戏
nepctf Nyan Cat 彩虹猫
Tinker's self-introduction
Certificate of SearchGuard configuration
Visual studio2019 配置使用pthread
轻松理解进程与线程
第六届蓝帽杯 EscapeShellcode
Here is a memorial
Some formulas for system performance and concurrency
Jetpack use exception problem collection
C语言-7月22日- NULL和nullptr的深入了解以及VScode对nullptr语句报错问题的解决
函数使用记录
C-8月1日-递归与动态内存管理
Node stepping on the pit 80 port is occupied
C语言-6月8日-求两个数的最小公倍数和最大公因数;判断一个数是否为完数,且打印出它的因子
杀死进程-查看防火墙状态
C语言-内存操作函数
swagger错误:WARN i.s.m.p.AbstractSerializableParameter - [getExample,421] - Illegal DefaultValue null
Day 79