当前位置:网站首页>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++;
}
}
}
}
}
边栏推荐
- 请求参数的发送和接收
- Query XML documents with XPath
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- Gif remove blank frame frame number adjustment
- LeetCode 515. 在每个树行中找最大值
- Facial expression recognition based on pytorch convolution -- graduation project
- 求组合数 AcWing 886. 求组合数 II
- Binary to decimal, decimal to binary
- 22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
- 推荐一个 yyds 的低代码开源项目
猜你喜欢

Debug debugging - Visual Studio 2022

剑指 Offer II 029. 排序的循环链表

Format - C language project sub file

20220630学习打卡

On a un nom en commun, maître XX.

AcWing 788. Number of pairs in reverse order

Parameters of convolutional neural network

too many open files解决方案

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

Concurrent programming (VI) ABA problems and solutions under CAS
随机推荐
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
Sword finger offer II 029 Sorted circular linked list
Summary of methods for counting the number of file lines in shell scripts
Divide candy (circular queue)
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
我們有個共同的名字,XX工
Parameters of convolutional neural network
[rust notes] 12 closure
Six dimensional space (C language)
How to use Jupiter notebook
Debug debugging - Visual Studio 2022
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
第一个Servlet
Log4j2 vulnerability recurrence and analysis
On a un nom en commun, maître XX.
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
20220630学习打卡
22-05-26 西安 面试题(01)准备
Deep parsing (picture and text) JVM garbage collector (II)
状态压缩DP AcWing 91. 最短Hamilton路径