LeetCode 75. Sort Colors(三颜色排序→K颜色排序)

题目描述:

    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.
    Note: you are not suppose to use the library's sort function for this problem.

分析:
    题意:给定n个有红、白、蓝三种颜色的物体,把它们按照红、白、蓝的颜色排序。在整型数组中,用0、1、2分别表示红、白、蓝三种颜色。不能使用排序函数。
    思路:这是一道经典的三颜色排序问题,可以采用双指针法解决。我们初始化指针red = 0、white = 1、blue = 2。Ⅰ. 如果white < blue且指针white指向2(蓝色),应该把蓝色换到后面去,即交换指针white和指针blue指向的值,同时blue减一;Ⅱ. 如果white > red且指针white指向0(红色),应该把红色换到前面去,即交换指针white和指针red指向的值,同时red加一。
    时间复杂度为O(n),空间复杂度为O(1)。
    这里给出两个版本代码(思路总体一致,第二个版本更优雅)。

代码:

#include <bits/stdc++.h>

using namespace std;

// T(n) = O(n)
// Accepted
class Solution {
public:
    void sortColors(vector<int>& nums) {
        int n = nums.size();
		// Exceptional Case: 
		if(n == 0){
			return;
		}
		// red, white, blue
		int red = 0, white = 0, blue = n - 1;
		// for case: [1, 0], white < blue will cause an error!
		while(white <= blue){
			if(nums[white] == 1){
				white++;
			}
			else if(nums[white] == 0){
				if(nums[red] != 0){
					swap(nums[red], nums[white]);
				}
				red++;
				white++;
			}
			else if(nums[white] == 2){
				if(nums[blue] != 2){
					swap(nums[white], nums[blue]);
				}
				blue--;
			}
		}
    }
};
代码:
#include <bits/stdc++.h>

using namespace std;

// T(n) = O(n)
class Solution {
public:
    void sortColors(vector<int>& nums) {
        int n = nums.size();
		// Exceptional Case: 
		if(n == 0){
			return;
		}
		int red = 0, blue = n - 1;
		for(int white = red; white <= blue; white++){
			while(nums[white] == 2 && white < blue){
				swap(nums[white], nums[blue--]);
			}
			while(nums[white] == 0 && white > red){
				swap(nums[red++], nums[white]);
			}
		}
	}
};

拓展:给定n个有k种颜色的物体,把它们按照k种颜色排序。在整型数组中,用0、1、2、...、k - 1分别表示k种颜色。不能使用排序函数。
    思路:这道题是三颜色排序的进化版本,k颜色排序题,总体还是采用双指针法,重复部分不再复述。假设当前排序考虑的颜色范围是[c_min,c_max],则它们初始值为c_min = 0、c_max = k - 1。一轮排序之后,两种颜色c_min = 0、c_max = k - 1已经处理完毕(分别交换至数组两端),接下来考虑[c_min + 1 = 1,c_max - 1 = k - 2]范围内的颜色排序,并重复该步骤直到c_min >= c_max,退出该循环。
    时间复杂度为O(n * k)。

代码:

#include <bits/stdc++.h>

using namespace std;

class Solution{
public:
	void sortColorsII(vector<int>& nums, int k){
		int n = nums.size();
		// exceptional case: 
		if(n <= 1 || k <= 1){
			return;
		}
		int c_min = 0, c_max = k - 1;
		int left = 0, right = n - 1;
		int i = 0;
		while(c_min < c_max){
			while(i <= right){
				if(nums[i] == c_min){
					swap(nums[left++], nums[i++]);
				}								
				else if(nums[i] == c_max){
					swap(nums[i], nums[right--]);
				}
				else{
					i++;
				}
			}
			i = left;
			c_min++;
			c_max--;
		}
	}
};
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值