当前位置:网站首页>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;
}
}
}
边栏推荐
- 【机器学习】朴素贝叶斯对文本分类--对人名国别分类
- If you want to grow rapidly, you must first experience a major blow!
- 普源示波器实际的使用效果怎么样
- [Ruiji takeout] day05 package management business development
- Aimbetter insight into your database, DPM and APM solutions
- 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
- Why is 0.1 + 0.2 not equal to 0.3? How to solve this problem?
- Mysql内置函数
- Static route and default route experiment
- 罗克韦尔AB PLC RSLogix数字量IO模块基本介绍
猜你喜欢

HCIP(13)

Brief introduction to PCB materials

If you want to grow rapidly, you must first experience a major blow!

记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题

SQL注入 Less34(POST型宽字节注入+布尔盲注)

HCIP(8)

腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实

Ordinary practice of JS DOM programming

Day3 classification management of Ruiji takeout project

CDN working principle
随机推荐
Differences of display values
局域网添加DNS服务器进行域名解析
静态路由和缺省路由实验
tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
Jianzhi offer II 062. implement prefix tree (medium design dictionary tree prefix tree string)
The applet listens for the target node to appear on the screen
Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022
SQL injection less34 (post wide byte injection + Boolean blind injection)
C语言编程规范学习笔记和总结
Add DNS server to LAN for domain name resolution
Ruiji takeout project - development of business development function Day2
软考网络工程师
Mysql内置函数
Apifox: satisfy all your fantasies about API
2021 mathematical modeling group B exercise
hcip实验(15)
What is a prime factor? In number theory, a prime factor (prime factor or prime factor) refers to a prime number that can divide a given positive integer
CDN working principle
ECMASript 5/6 笔记