当前位置:网站首页>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++;
}
}
}
}
}
边栏推荐
- 记忆化搜索 AcWing 901. 滑雪
- JS ternary operator - learning notes (with cases)
- Using variables in sed command
- Final review of Database Principles
- Concurrent programming (III) detailed explanation of synchronized keyword
- Analysis of Alibaba canal principle
- LeetCode 715. Range 模块
- 剑指 Offer II 029. 排序的循环链表
- Six dimensional space (C language)
- 樹形DP AcWing 285. 沒有上司的舞會
猜你喜欢
Vscode connect to remote server
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
即时通讯IM,是时代进步的逆流?看看JNPF怎么说
Concurrent programming (VI) ABA problems and solutions under CAS
Alibaba canaladmin deployment and canal cluster Ha Construction
推荐一个 yyds 的低代码开源项目
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
AcWing 786. 第k个数
Monotonic stack -84 The largest rectangle in the histogram
LeetCode 30. 串联所有单词的子串
随机推荐
TP5 multi condition sorting
LeetCode 715. Range 模块
浅谈企业信息化建设
<, < <,>, > > Introduction in shell
[rust notes] 12 closure
Basic knowledge of network security
Shell script kills the process according to the port number
Deeply understand the underlying data structure of MySQL index
Method of intercepting string in shell
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Complex character + number pyramid
数位统计DP AcWing 338. 计数问题
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
Character pyramid
Gif remove blank frame frame number adjustment
ES6 promise learning notes
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
DOM 渲染系统(render mount patch)响应式系统
Summary of methods for counting the number of file lines in shell scripts
Slice and index of array with data type