75. Sort Colors
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的更多相关文章
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和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 ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 【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 ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 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 OJ 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(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 常用tcode
SAP常用TCODE 1 MMBE 查询库存 2 CO01 生产订单创建 3 ME2N-按采购订单编号 ME2B/ME2M/ME2C/ME2W 采购订单查询 清单范围ALV 4 MB51 物料凭证清单 ...
- raido 赋值第一次成功,然后就赋值不显示
$("#id").attr("checked",true); //显示出现问题,第一次成功 $("#id").prop("chec ...
- ZOJ 1078 Palindrom Numbers
原题链接 题目大意:判断一个数是不是palindrom.不限于十进制,可以在任何进制下判断. 解法:还好,数字的范围不大,int类型足够搞定.方法就是从2进制开始,先把数字转换成2进制,判断是否对称, ...
- JS初学之-点击元素,当前的显示样式,其他变灰色
点击按钮或者其他元素,当前的变化,其他的不变(比如选项卡按钮,点击当前的变为黄色,其他的不变色),这样的情况我们有两种思路: 1.全部清空,当前添加 for(var i=0;i<aBtn.len ...
- 工作中遇到的问题--BindException
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResu ...
- hdu1025 dp(最长上升子序列LIS)
题意:有一些穷国和一些富国分别排在两条直线上,每个穷国和一个富国之间可以建道路,但是路不能交叉,给出每个穷国和富国的联系,求最多能建多少条路 我一开始在想有点像二分图匹配orz,很快就发现,当我把穷国 ...
- linux oracle profile配置
[oracle@db01 ~]$ more .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashrc ...
- 一个PHP数组能占多大内存
最近用PHP读取一个大文件把相关数据存放到数组中,之后处理并输出, 读取过程中发现占用内存很大, 于是很好奇这个问题. 简单的写一个代码 <?php $m1 = memory_get_usage ...
- Java 异常处理机制和集合框架
一.实验目的 掌握面向对象程序设计技术 二.实验环境 1.微型计算机一台 2.WINDOWS操作系统,Java SDK,Eclipse开发环境 三.实验内容 1.Java异常处理机制涉及5个关键字:t ...
- wikioi 1973 Fibonacci数列【输出第N项的值】
/*===================================== 1978 Fibonacci数列 3 题目描述 Description 斐波纳契数列是这样的数列: f1 = 1 f2 ...