给定一个包含红色、白色和蓝色,一共 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. CSIC_716_20191029【人脸打分系统】

    今日内容: 1.调用百度的AI接口,完成人脸图像打分( 敷衍) 2.完成系统内置时间的打印 3.将上述两段代码生成可执行文件 ------------------------------------- ...

  2. 单行文本截断 text-overflow

    div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  3. Altera: set pin locations using tcl

    1, compile the project; 2, store current tcl settings: Project –> Generate Tcl File from Project- ...

  4. scala中类的简单使用记录

    import scala.collection.mutable.ArrayBuffer /** * scala 中内部类的使用 */ class Classes { class Stu(name:St ...

  5. ForkJoin学习笔记

    1.Fork/Join框架:(分治算法思想) 在必要的情况下,将一个大任务,进行拆分(fork) 成若干个子任务(拆到不能再拆,这里就是指我们制定的拆分的临界值),再将一个个小任务的结果进行join汇 ...

  6. 重装系统后配置原有的mysql

    1.重装系统后配置原有的mysql 2.修改 my.ini [修改 basedir:MySQL当前所在路径 datadir  数据存放路径] [mysqld] # 设置3306端口 port= # 设 ...

  7. eclipse查看源码的配置

    1.打开eclipse软件,点击window-preference 2.在弹出框中选择java-Installed JRES,选中的默认就行,然后点一下选中的,点击edit 3.弹出框中选择第二个,展 ...

  8. 修改web项目的启动页

    修改web项目的启动页

  9. id(), is, ==, 的区别与小数据池

    1. id() 内存地址 s = 'asdf' n = id(s) print(n)输出:16506464 #16506464为变量s的内存地址 2. == 比较数值 3. is 比较内存地址 数字, ...

  10. day 65 Django基础之django分页

      Django基础之django分页   一.Django的内置分页器(paginator) view from django.shortcuts import render,HttpRespons ...