Sort Colors(荷兰旗问题)

75. Sort Colors

My Submissions
Total Accepted: 94286  Total Submissions: 272931  Difficulty: Medium

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.

click to show follow up.

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems


第一种方法:最直接的方法
(1)就是先遍历数组一遍,记录0、1、2的个数x、y、z
(2)遍历数组,依次赋值数组x个0,y个1,z个2
class Solution
{
public:
    void sortColors(vector<int>& nums)
    {
        int red = 0, white = 0, blue = 0;
        for (auto color : nums)
        {
            if (color == 0)
                ++red;
            else if (color == 1)
                ++white;
            else
                ++blue;
        }
        
        int i;
        for (i = 0; i < red; ++i)
            nums[i] = 0;
        for (; i < red + white; ++i)
            nums[i] = 1;
        for (; i < nums.size(); ++i)
            nums[i] = 2;
    }
};


第二种方法:有点快速排序的味道
(1)red 记录从头开始 0 的下一个位置
(2)blue 记录从末尾开始 2 的下一个位置
(2)从头开始遍历数组,到 blue 位置结束

class Solution
{
public:
    void sortColors(vector<int>& nums)
    {
        int red = 0, blue = nums.size() - 1;

        int i = 0; //注意条件, i <= blue ,容易写成 i < nums.size()
        while (i<= blue)
        {
            //把 0 放到对应的 red 位置后,nums[i] 的位置还有可能是 2,所以不能 ++i
            if (nums[i] == 0)
            {
                if (i == red) {
                    ++i;
                    ++red;
                }
                else {
                    Swap(nums[i], nums[red]);
                    ++red;
                }
            }
            
            //把 2 放到对应的 blue 位置后,nums[i] 的位置还有可能是 1,所以不能 ++i
            else if (nums[i] == 2)
            {
                Swap(nums[i], nums[blue]);
                --blue;
            }
            
            // 只有当 nums[i] == 1 的时候,i 往前移动
            else
            {
                ++i;
            }
        }
    }

    void Swap(int &a, int &b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
};



荷兰问题是一个经典的排序问题,它要求按照荷兰的颜色顺序(红、白、蓝)对一个包含红、白、蓝三种颜色的数组进行排序。 快速排序算法可以用来解决荷兰问题。该算法基于分治的思想,通过多次划分和交换元素来达到排序的目的。以下是快速排序算法解决荷兰问题的步骤: 1. 选择一个枢轴元素(一般是数组的最后一个元素)。 2. 初始化三个指针:low指向数组的起始位置,high指向数组的末尾位置,mid指向数组的起始位置。 3. 从头遍历数组,如果当前元素小于枢轴元素,则交换当前元素和mid指针指向的元素,并将mid指针后移一位。 4. 如果当前元素等于枢轴元素,则将high指针前移一位。 5. 如果当前元素大于枢轴元素,则交换当前元素和high指针指向的元素,并将high指针前移一位。 6. 重复步骤3到步骤5,直到low指针和high指针相遇为止。 7. 最后,将枢轴元素放在mid指针的位置上,这样数组就被分成了三个部分:小于枢轴元素的部分、等于枢轴元素的部分和大于枢轴元素的部分。 8. 递归地对小于和大于枢轴元素的部分进行排序。 以下是一个示例荷兰问题的快速排序的实现(使用Python语言): ```python def dutch_flag_sort(arr): def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] def quicksort(arr, low, high): if low < high: pivot = arr[high] mid = low for i in range(low, high): if arr[i] < pivot: swap(arr, i, mid) mid += 1 swap(arr, mid, high) quicksort(arr, low, mid - 1) quicksort(arr, mid + 1, high) quicksort(arr, 0, len(arr) - 1) return arr # 示例用法 arr = [2, 0, 2, 1, 1, 0] sorted_arr = dutch_flag_sort(arr) print(sorted_arr) ``` 上述示例代码会输出 `[0, 0, 1, 1, 2, 2]`,即按照荷兰顺序排序的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值