当前位置:网站首页>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++;
}
}
}
}
}
边栏推荐
- Low code momentum, this information management system development artifact, you deserve it!
- 樹形DP AcWing 285. 沒有上司的舞會
- Monotonic stack -42 Connect rainwater
- Introduction to the usage of getopts in shell
- LeetCode 30. 串联所有单词的子串
- createjs easeljs
- Apache startup failed phpstudy Apache startup failed
- In the digital transformation, what problems will occur in enterprise equipment management? Jnpf may be the "optimal solution"
- Es8 async and await learning notes
- Divide candy (circular queue)
猜你喜欢
浅谈企业信息化建设
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
精彩回顾|I/O Extended 2022 活动干货分享
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
Log4j2 vulnerability recurrence and analysis
LeetCode 30. 串联所有单词的子串
Gif remove blank frame frame number adjustment
Final review of Database Principles
Monotonic stack -42 Connect rainwater
[concurrent programming] explicit lock and AQS
随机推荐
数位统计DP AcWing 338. 计数问题
Introduction to the usage of getopts in shell
Deep parsing JVM memory model
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
Using variables in sed command
Arbre DP acwing 285. Un bal sans patron.
C language file reading and writing
Tree DP acwing 285 A dance without a boss
高斯消元 AcWing 883. 高斯消元解线性方程组
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
Shell script kills the process according to the port number
Drawing maze EasyX library with recursive backtracking method
php public private protected
Format - C language project sub file
LeetCode 515. 在每个树行中找最大值
我们有个共同的名字,XX工
Annotations simplify configuration and loading at startup
The difference between if -n and -z in shell
Allocation exception Servlet
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?