当前位置:网站首页>75. Color classification (medium array double pointer sorting)
75. Color classification (medium array double pointer sorting)
2022-07-28 22:25:00 【Calm in the wind and rain】
75. Color classification
Give one that contains red 、 White and blue 、 common n Array of elements nums , Sort them in place , Make elements of the same color adjacent , And follow the red 、 white 、 In blue order .
We use integers 0、 1 and 2 Red... Respectively 、 White and blue .
Must be in a library that does not use sort Function to solve this problem .
Example 1:
Input :nums = [2,0,2,1,1,0]
Output :[0,0,1,1,2,2]
Example 2:
Input :nums = [2,0,1]
Output :[0,1,2]
Tips :
n == nums.length
1 <= n <= 300
nums[i] by 0、1 or 2
Advanced :
Can you solve this problem without using the sort function in the code base ?
Can you come up with a one pass scanning algorithm using only constant space ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/sort-colors
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Take advantage of the fast-paced partition The second condition for the function idea to complete the advanced order .
Traversal array , With a double pointer zero and two Locate the end of the 0 and At the beginning 2( Traverse it 2 Put them at the end in order ). The traversal pointer variable is i, When you meet 0 The exchange zero and i The number at , take 0 In order to the beginning , When you meet 2, In exchange for two and i Number of places , take 2 Order to the end , encounter 1 direct i++ that will do .
Answer key (Java)
class Solution {
public void sortColors(int[] nums) {
int len = nums.length;
if (len < 2) {
return;
}
int i = 0, zero = 0, two = len;//two It can also be initialized to len - 1
while (i < two) {
if (nums[i] == 0) {
swap(nums, i, zero);
zero++;
i++;
} else if (nums[i] == 1) {
i++;
} else {
two--;
swap(nums, i, two);
}
}
}
private void swap(int[] nums, int index1, int index2) {
if (index1 != index2) {
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
}
}
}
边栏推荐
- [Ruiji takeout] day05 package management business development
- Ordinary practice of JS DOM programming
- 静态成员static详解
- Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)
- 【机器学习】朴素贝叶斯对文本分类--对人名国别分类
- Learn kotlin - extension function
- [machine learning] naive Bayesian classification of text -- Classification of people's names and countries
- HCIP(14)
- Hcip experiment (15)
- Embrace open source guidelines
猜你喜欢

AimBetter洞察您的数据库,DPM 和 APM 解决方案

Learning notes and summary of C language programming specification

Mysql内置函数

成立不到一年!MIT衍生量子计算公司完成900万美元融资

How to establish a decentralized community in Web3

HCIP(15)
![[Ruiji takeout project]day4 - dish management](/img/2a/2d9deb7a583aa37b38a67ef2c74ee7.png)
[Ruiji takeout project]day4 - dish management

Alibaba cloud CDN practice
![[CS231N]Lecture_2:Image Classification pipelin](/img/4f/de56b071560ada746c587a9dbc5f02.jpg)
[CS231N]Lecture_2:Image Classification pipelin

40. Combined sum II
随机推荐
hcip实验(12)
Data visualization news, different forms of news reports
SQL注入 Less34(POST型宽字节注入+布尔盲注)
[cloud native kubernetes] mapping external service under kubernetes cluster eendpoint
[binary tree] pseudo palindrome path in binary tree
What does GPRS network mean
CDN工作原理
【NLP】生成词云
DHCP和PPPoE协议以及抓包分析
HCIP(10)
2021数学建模B组练习
【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
Lotus 1.16.0 extend sector expiration time
静态路由和缺省路由实验
Learn kotlin - extension function
Differences of display values
How to establish a decentralized community in Web3
示波器发展史中的变化
成立不到一年!MIT衍生量子计算公司完成900万美元融资