[leetcode]75. Sort Colors

原创 2016年05月31日 00:11:52

题目地址

https://leetcode.com/problems/sort-colors/

题目大意

一个含有n个整数数组,元素由0 1 2构成,排列随机。在O(n)时间复杂度,O(1)空间复杂度下,将其按升序排序。

解题思路

解一

首先遍历一遍,记录0,1,2个数,然后重新填充数组。

解二

使用两个标记为,a记录距离数组起始最近的非0元素,b记录距离数组尾最近的非2元素。遍历两个标记之间,遇到0与a标记元素交换,遇到2与b标记元素交换。

代码

解一

class Solution {
public:
    void sortColors(vector<int>& nums) {
        if (nums.size() == 0) {
            return;
        }
        int red = 0;
        int white = 0;

        for (int i = 0; i != nums.size(); ++i) {
            if (nums[i] == 0) {
                ++red;
            }
            if (nums[i] == 1) {
                ++white;
            }
        }

        for (int i = 0; i != nums.size(); ++i) {
            if (red != 0) {
                nums[i] = 0;
                --red;
            } else if (white != 0) {
                nums[i] = 1;
                --white;
            } else {
                nums[i] = 2;
            }
        }
        return;
    }
};

解二

class Solution {
public:
    void sortColors(vector<int>& nums) {
        if (nums.empty()) {
            return;
        }
        int size = nums.size();
        int start = 0;
        while (start < size && nums[start] == 0) {
            start++;
        }

        int end = size - 1;
        while (end >= 0 && nums[end] == 2) {
            end--;
        }

        int i = start;
        while (start < end && i <= end && start < size && end >= 0) {
            if (nums[i] == 1) {
                ++i;
                continue;
            }
            if (nums[i] == 0) {
                nums[i] = nums[start];
                nums[start] = 0;
                start++;
            } else if (nums[i] == 2){
                nums[i] = nums[end];
                nums[end] = 2;
                end--;
                if (nums[start] == 0) {
                    start++;
                    i++;
                }
            }
        }
    }
};
版权声明:本作品由掠雪墨影创作,采用知识共享署名 4.0 国际许可协议进行许可。转载请以链接形式标明本文地址。

相关文章推荐

leetcode-75 Sort Colors

因为待排序的数字的大小在一个很小的范围之内(0-2),所以可以使用这种算法: A rather straight forward solution is a two-pass algorithm u...

[leetcode] 75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...

LCP075 LeetCode 75. Sort Colors

Total Accepted: 100513 Total Submissions: 287992 Difficulty: Medium Given an array with n...

Leetcode 75:Sort Colors

Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of ...

leetcode:75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...

Leetcode75 - Sort Colors(two pointers)

题意给定一个数组,只包含0,1,2,将0,1,2按照顺序排列。要求: 1. 只能开常数空间 2. 对数组只扫描一遍思路算法1算法1是最容易想到的,我们扫描一遍数组统计一下每个数字的数目,然后再扫描...
  • Lzedo
  • Lzedo
  • 2017-01-19 23:17
  • 87

[leetcode] 75. Sort Colors 解题报告

题目链接:https://leetcode.com/problems/sort-colors/ Given an array with n objects colored red, white o...

LeetCode 75 Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same colo...

LeetCode(75)Sort Colors (荷兰三色旗问题 Dutch National Flag)

题目如下: The flag of the Netherlands consists of three colours: red, white and blue. Given balls of the...

[leetCode 75] Sort Colors

题目链接:sort-colors import java.util.Arrays; /** * Given an array with n objects colored red...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)