给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。

注意:

不能使用代码库中的排序函数来解决这道题。

示例:

输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2]

进阶:

  • 一个直观的解决方案是使用计数排序的两趟扫描算法。

首先,迭代计算出0、1 和 2 元素的个数,然后按照0、1、2的排序,重写当前数组。

  • 你能想出一个仅使用常数空间的一趟扫描算法吗?

双指针

class Solution {
public:
void sortColors(vector<int>& nums)
{
int len = nums.size();
int low = 0;
int high = len - 1;
int i = 0;
while(i <= high)
{
if(nums[i] == 0)
{
int temp = nums[low];
nums[low] = nums[i];
nums[i] = temp;
i++;
low++;
}
else if(nums[i] == 1)
{
i++;
}
else if(nums[i] == 2)
{
int temp = nums[high];
nums[high] = nums[i];
nums[i] = temp;
high--;
}
}
}
};

Leetcode75. Sort Colors颜色分类的更多相关文章

  1. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  2. [LeetCode] Sort Colors 颜色排序

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

  3. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

  4. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  5. LeetCode75 Sort Colors

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

  6. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  7. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  8. Java for LintCode 颜色分类

    给定一个包含红,白,蓝且长度为n的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数0,1和2分别代表红,白,蓝. 解题思路: Java for Leet ...

  9. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

随机推荐

  1. 廖雪峰Java16函数式编程-2Stream-7其他操作

    1. 排序 Stream<T> sorted(); //按元素默认大小排序(必须实现Comparable接口) Stream<T> sorted(Comparator<? ...

  2. CSS案例3(在线教育网站)

    案例练习目的是总结以前的css和html 还有ps的使用. 制作步骤: 准备相关文件.(内部样式表) html文件(index.html) 图片文件 准备CSS 初始化. 书写结构和样式 确定版心(是 ...

  3. RocketMQ源码分析之RocketMQ事务消息实现原下篇(事务提交或回滚)

    摘要: 事务消息提交或回滚的实现原理就是根据commitlogOffset找到消息,如果是提交动作,就恢复原消息的主题与队列,再次存入commitlog文件进而转到消息消费队列,供消费者消费,然后将原 ...

  4. Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄

    有了回调函数的概念及上面的例子,我们可以继续了.其实想要找到一个标题已知的窗口句柄,用一个API函数就可以了:FindWindow.其函数原形是:function FindWindow(lpClass ...

  5. Windows便筏快捷键

    Ctrl + L:左对齐 Ctrl + E:居中对齐 Ctrl + R:右对齐 Ctrl + B:加粗 Ctrl + I:斜体 Ctrl + U:给文字添加下划线 Ctrl + T: 给文字添加删除线 ...

  6. 万恶之源-python介绍

    PATH OF PYTHON (生命短暂,我要学pythonヾ(◍°∇°◍)ノ゙) 一.Python介绍: 简史:Python诞生于1989年的圣诞节, 创始人为Guido van Rossum, 又 ...

  7. 手把手教你 GitLab 的安装及使用(转)

    深山田 关注 2018.01.30 22:58 字数 1696 阅读 15559评论 2喜欢 15 前言 新入职公司,发现公司还在使用落后生产工具 svn,由于重度使用过 svn 和 git ,知道这 ...

  8. 矩阵连乘 /// 区间DP oj1900

    题目大意: 输入t :t为测试用例个数 接下来t个测试 每个测试用例 第一行输入n: n为矩阵个数 保证n个矩阵依序是可乘的 接下来n行 每行输入p,q:p为长度q为宽度 对给定的n个矩阵确定一个计算 ...

  9. UMP系统功能 读写分离

  10. nginx 配置文件备份 nginx.conf and vhosts

    bogon:vhosts xingchong$ brew services restart nginx Stopping `nginx`... (might take a while) ==> ...