当前位置:网站首页>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;
}
}
}
边栏推荐
- Written examination summary record
- 【云原生之kubernetes】在kubernetes集群下的映射外部服务—Eendpoint
- 想要快速成长,先要经历重大打击!
- 【二叉树】二叉树中的伪回文路径
- Hcip experiment (12)
- SQL injection less34 (post wide byte injection + Boolean blind injection)
- The applet listens for the target node to appear on the screen
- HCIP(8)
- The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
- 小程序 组件 定时器的清除
猜你喜欢

Future trend of defi in bear market

SQL注入 Less42(POST型堆叠注入)

Static route and default route experiment

DHCP和PPPoE协议以及抓包分析

Sword finger offer II 053. Medium order successor in binary search tree (medium binary search tree DFS)

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

Hcip experiment (14)

Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)

SQL injection less38 (Stack Injection)

AimBetter洞察您的数据库,DPM 和 APM 解决方案
随机推荐
Mysql内置函数
Lotus 1.16.0 extend sector expiration time
Basic introduction of Rockwell AB PLC rslogix digital quantity IO module
TensorFlow Serving 高性能的机器学习模型服务系统
Principle of object. Prototype. ToString. Call()
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
Learn kotlin - extension function
腾讯云数据库负责人借了一亿元炒股?知情人士:金额不实
Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
Embrace open source guidelines
Netease Yunxin 2022q2 product supply station, come and get your product supply plan!
小程序 组件 定时器的清除
静态路由和缺省路由实验
[CS231N]Lecture_ 2:Image Classification pipelin
【二叉树】二叉树中的伪回文路径
静态成员static详解
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
HCIP(12)
IFLYTEK written examination
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)