Leetcode - Sort Colors

本文介绍了一种高效的三色排序算法,该算法仅需遍历数组一次即可完成排序,适用于最多只有三种不同数值的数组。文章详细解析了算法背后的原理及其实现过程,并对比了两种不同的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

[分析] 此题就是排序至多有三种值的数组。自己的思路是通过遍历数组两次排序,第一次遍历将 0 全部放置在最前面一段,第二次遍历数组剩余元素将 1 放置在前面。 但这样做总觉得缺了什么,一定有只要遍历一次的做法。搜了下,果然。[url]http://blog.youkuaiyun.com/zxzxy1988/article/details/8596144[/url] 即使遍历两次还有另一种非常朴素的做法,第一次遍历计数0,1,2 的出现个数,第二次遍历就是根据计数赋值数组。
仅遍历一次的思路参考博文中分析得非常到位,尤其是三指针含义的解释以及更新规则。low指示1元素的开始,是0和1的分界线,high指示1元素的结尾,是1和2的分界线,因为low指向的元素是1,所以每次和mid交换后low 和 mid均要递增,而high指针指向的元素可能是0可能是1,换句话说,[mid, high]之间是尚未遍历的区域, 因此high 和mid交换后mid不能递增。


public class Solution {
// Method 2: scan only 1 time
public void sortColors(int[] nums) {
if (nums == null || nums.length == 0)
return;
int low = 0, mid = 0, high = nums.length - 1;
while (mid <= high) {
switch (nums[mid]) {
case 0:
swap(nums, low++, mid++); break;
case 1:
mid++; break;
case 2:
swap(nums, high--, mid); break;
default:
break;
}
}
}

// Method 1: scan 2 times
public void sortColors1(int[] nums) {
if (nums == null || nums.length == 0)
return;
int N = nums.length;
int i = reorder(nums, 0, 0);
if (i == N) return;
reorder(nums, i, 1);
}
private int reorder(int[] nums, int i, int t) {
int N = nums.length;
while (i < N && nums[i] == t) {
i++;
}
int j = i;
while (true) {
// find next 0
while (j < N && nums[j] > t) {
j++;
}
if (j == N) break;
swap(nums, i, j);
i++;
}
return i;
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值