当前位置:网站首页>LeetCode 75. Color classification
LeetCode 75. Color classification
2022-07-03 09:02:00 【Sasakihaise_】
【 Count sorting 】 Because there are only three colors , It can directly record the number of the three colors , And then from 0 To 1 Assign a value to the original array .
class Solution {
public void sortColors(int[] nums) {
int[] cnt = new int[3];
for (var x: nums) {
cnt[x]++;
}
int i = 0, j = 0;
while (j < nums.length) {
while (cnt[i] == 0) {
i++;
}
nums[j++] = i;
cnt[i]--;
}
}
}【 Double pointer + Two scans 】 With a pointer i Enumeration elements , The pointer j Point not to 0 The elements of , Every time i Enumerate to 0 Time and pointer j Exchange elements . You can skip the previous one during the second scan 0, Continue to use this method to traverse .
class Solution {
// Double pointer + Second traversal
public void sortColors(int[] nums) {
int i = 0, j = 0, n = nums.length;
for (i = 0; i < n; i++) {
if (nums[i] == 0) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
j++;
}
}
j = 0;
while (j < n && nums[j] == 0) {
j++;
}
for (i = j; i < n; i++) {
if (nums[i] == 1) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
j++;
}
}
}
}【 Three pointers + A scan 】 use j and k Respectively point to exchange into 0 and 1 The location of ,i Continue enumerating all elements . When i encounter 1 When and k Element exchange at , When i encounter 0 Time and j Element exchange at , But this will cause a problem that the elements exchanged to the back may be 1, If there is a lot of separation in the middle 2, Then the order is wrong , So here is another judgment .
Take up a
2,0,2,1,1,0 i = j = k = 0
1.i=0 The time is 2, Never mind
2.i=1 The time is 0, and j=0 Exchange becomes 0,2,2,1,1,0, Because the exchange back is 2 Never mind ;j=1,k=1
3.i=2 The time is 2, Never mind
4.i=3 The time is 1, and k=1 Exchange becomes 0,1,2,2,1,0,k=2
5.i=4 The time is 1, and k=2 Exchange becomes 0,1,1,2,2,0,k=3
6.i=5 The time is 0, and j=1 Exchange becomes 0,0,1,2,2,1, Because the exchange back is 1, So continue with k=3 Exchange becomes 0,0,1,1,2,2
class Solution {
// Three pointers + One traverse 10:25 10:35
public void sortColors(int[] nums) {
int i = 0, j = 0, k = 0, n = nums.length;
for (i = 0; i < n; i++) {
if (nums[i] == 1) {
int t = nums[i];
nums[i] = nums[k];
nums[k] = t;
k++;
} else if (nums[i] == 0) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
j++;
if (k < j) k = j;
if (nums[i] == 1) {
t = nums[i];
nums[i] = nums[k];
nums[k] = t;
k++;
}
}
}
}
}
边栏推荐
- 22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
- Summary of methods for counting the number of file lines in shell scripts
- 注解简化配置与启动时加载
- 使用dlv分析golang进程cpu占用高问题
- [rust notes] 13 iterator (Part 1)
- String splicing method in shell
- Markdown learning
- Mortgage Calculator
- 状态压缩DP AcWing 291. 蒙德里安的梦想
- Using DLV to analyze the high CPU consumption of golang process
猜你喜欢

Sword finger offer II 029 Sorted circular linked list

Dom4j traverses and updates XML

Final review of Database Principles

Memory search acwing 901 skiing

Concurrent programming (VI) ABA problems and solutions under CAS

MySQL three logs

Six dimensional space (C language)

Character pyramid

浅谈企业信息化建设

Try to reprint an article about CSDN reprint
随机推荐
Concurrent programming (VI) ABA problems and solutions under CAS
JS non Boolean operation - learning notes
树形DP AcWing 285. 没有上司的舞会
What is the difference between sudo apt install and sudo apt -get install?
传统企业数字化转型需要经过哪几个阶段?
Divide candy (circular queue)
Mortgage Calculator
[concurrent programming] explicit lock and AQS
Gif remove blank frame frame number adjustment
LeetCode 871. 最低加油次数
Vscode connect to remote server
LeetCode 1089. Duplicate zero
On the difference and connection between find and select in TP5 framework
Noip 2002 popularity group selection number
LeetCode 75. 颜色分类
php public private protected
AcWing 787. 归并排序(模板)
[concurrent programming] concurrent security
Using variables in sed command
Find the combination number acwing 885 Find the combination number I