• [LeetCode]98. SortColors颜色排序


    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.

    Follow up:
    A rather straight forward solution is a two-pass algorithm using counting sort.
    First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.

    Could you come up with an one-pass algorithm using only constant space?

    Subscribe to see which companies asked this question

    解法1:显然直接排序是最简便的一种方法。不让使用库函数,懒得写排序算法直接上了。

    class Solution {
    public:
        void sortColors(vector<int>& nums) {
             sort(nums.begin(), nums.end());
        }
    };

    解法2:可以借助快速排序的划分思想,对输入做两次划分即可:第一次key=1,将所有<key的拿到数组前面;第二次key=2,将后半部分未排序数组所有<key的拿到后半部分数组的前面。

    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int n = nums.size();
            if(n < 2) return;
            int pos = partition(nums, 0, n - 1, 1);
            partition(nums, pos, n - 1, 2);      
        }
    private:
        int partition(vector<int>& nums, int low, int high, int key) {
            int n = nums.size();
            while(low < high) {
                while(low < n && nums[low] < key) ++low;
                while(high >= 0 && nums[high] >= key) --high;
                if(low < n && high >= 0 && low < high)
                    swap(nums[low], nums[high]);
                ++low;
                --high;
            }
            return --low;
        }
    };

    解法3:一个更简便的方法是扫描输入,统计每个数出现的次数,然后再按次数写入这些数到输入里即可。

    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int num[3] = {0, 0, 0};
            for(int i = 0; i < nums.size(); ++i) 
                ++num[nums[i]];
            int k = 0;
            for(int i = 0; i < 3; ++i)
                while(num[i]-- > 0) nums[k++] = i;
        }
    };

    解法4:初始化两个指针beg和end指向数组的头和尾,然后从头遍历数组,若遇到0,则交换当前值和beg所指值,且beg前移一步;若遇到1则继续;若遇到2则交换当前值和end所指值,且end后移一步。直到遍历到end指针处结束。

    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int nums1 = 0, beg = 0, end = nums.size() - 1;
            for(int i = 0; i <= end; ++i) {
                if(nums[i] == 0) swap(nums[i], nums[beg++]);
                if(nums[i] == 2) swap(nums[i--], nums[end--]); //注意交换后当前位置还需要判断,需要--i
            }
        }
    };
  • 相关阅读:
    荔枝派Nano (Lichee Pi)玩 Linux 傻瓜教程 (5) --- 虚拟USB网卡,SSH登录
    荔枝派Nano (Lichee Pi)玩 Linux 傻瓜教程 (4) --- 安装Python
    荔枝派Nano (Lichee Pi)玩 Linux 傻瓜教程 (3) --- TF卡扩容
    荔枝派Nano (Lichee Pi)玩 Linux 傻瓜教程 (2) --- 文件传输
    荔枝派Nano (Lichee Pi)玩 Linux 傻瓜教程 (1) --- 烧录,数据线连接登录
    RETRO MACHINE(迷你FC街机/掌机)Dump及改造
    Jetson (4)--- 人脸识别(OpenCV安装)
    Jetson (3)--- 人脸识别(Dlib安装)
    Jetson (2)--- 环境配置
    Jetson (1)--- 金属盒子按键Power,Reset线接法
  • 原文地址:https://www.cnblogs.com/aprilcheny/p/5020412.html
Copyright © 2020-2023  润新知