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

==========

有三种颜色的物体若干,颜色分别是red,waite,blue,分别使用数组0,1,2来表示.

要求是:将这些物体按照颜色red,waite,blue排序.

=======

思路:

采用快速排序的思路:

begin:指向将要排序为red(0)的位置

end:指向将要排序为blue(2)的位置

curr:遍历指针,

 如果当前元素为0,则和begin位置swap,通知begin++,curr++

 如果当前元素为1,curr++

 如果当前元素为2,则和end位置swap,但是curr元素不能增加,end--

====

code:

class Solution {
public:
void sortColors(vector<int>& nums) {
int n = nums.size();
int curr,begin,end;
curr = begin = ;
end = n-;
while(curr<=end){
if(nums[curr]==){
swap(nums[begin++],nums[curr++]);
}else if(nums[curr]==){
curr++;
}else if(nums[curr]==){
swap(nums[end],nums[curr]);
end--;
}
}
}
};

75. Sort Colors的更多相关文章

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

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

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

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

  3. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  4. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  5. 【LeetCode】75. Sort Colors (3 solutions)

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

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

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

  7. Leetcode 75. Sort Colors

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

  8. LeetCode OJ 75. Sort Colors

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

  9. LeetCode 75. Sort Colors(排序颜色)

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

随机推荐

  1. CentOS6下搭建OpenVPN服务器

    • OpenVPN简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.使用OpenVPN可以方便地在家庭.办公场所.住宿酒店等不同网 ...

  2. sql语句 优化技巧

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...

  3. c#实现高精度四舍五入

    /// <summary>        /// 實現數據的四捨五入        /// </summary>        /// <param name=" ...

  4. boot/head.S

    /* * linux/boot/head.S * * Copyright (C) 1991, 1992 Linus Torvalds */ /* * head.S contains the 32-bi ...

  5. python3获取当前目录(转)

    转自:http://www.elias.cn/Python/GetPythonPath?from=Develop.GetPythonPath 1.  以前的方法 如果是要获得程序运行的当前目录所在位置 ...

  6. leetcode 137. Single Number II ----- java

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  7. URAL 2030 Awesome Backup System

    Awesome Backup System Time limit: 2.0 secondMemory limit: 64 MB It is known that all people can be d ...

  8. IOS开发之SWIFT

    Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了 ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在其中 ...

  9. GNU C 扩展(转)

    GNU CC 是一个功能非常强大的跨平台 C 编译器,它对 C 语言提供了很多扩展,这些扩展对优化.目标代码布局.更安全的检查等方面提供了很强的支持.这里对支持支持 GNU 扩展的 C 语言成为 GN ...

  10. [codeforces 391D2]Supercollider

    这题觉得似乎不难的样子…… 但硬是没有做出来,真是不知说什么好喵~ 注意到没有两条共线的线段具有公共点,没有重合的线段 说明每个十字形最多涉及一个水平线段和一个竖直线段 这说明我们可以二分了:每条线段 ...