zoukankan      html  css  js  c++  java
  • [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
            }
        }
    };
  • 相关阅读:
    adb入门学习笔记
    adb连接手机模拟器
    burp抓取手机模拟器流量
    Windows 下安装drozer(Windows 10),连接手机(红米note4X)
    UnicodeEncodeError: 'ascii' codec can't encode characters in position
    Windows下的Nessus安装与启动
    对VAuditDemo的一次审计
    http协议
    namp详解
    sqlmap详解
  • 原文地址:https://www.cnblogs.com/aprilcheny/p/5020412.html
Copyright © 2011-2022 走看看