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 ...
随机推荐
- js部分---运算符,if分支语句,for循环;switch case 的用法;
------------------------------------------运算符---------------------------------------------------- *数 ...
- JavaWeb学习记录(二十五)——权限管理总结
一.面向对象思想简化数据库操作 public List<Role> getObjectsByIds(List<AdminRole> adminRoles) { L ...
- 在线QQ客服 生成
很简单,分为个人QQ和企业QQ: 一:企业QQ代码: http://crm2.qq.com/page/portalpage/wpa.php?uin=123456&aty=1&a=0&a ...
- 【BZOJ1010】【HNOI2008】玩具装箱
继续看黄学长代码 原题: P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1.. ...
- Docker Resources
Menu Main Resources Books Websites Documents Archives Community Blogs Personal Blogs Videos Related ...
- Top Things to Consider When Troubleshooting Complex Application Issues
http://blogs.msdn.com/b/debuggingtoolbox/archive/2011/10/03/top-things-to-consider-when-troubleshoot ...
- JSBinding + SharpKit / 原理篇:Delegate
以 NGUI 的 UIEventListener 为例: 有一个类: using SharpKit.JavaScript; using UnityEngine; using System.Collec ...
- LOMO效果
//LOMO效果 public static Bitmap changeToLomo(Bitmap bitmap) { int width = bitmap.getWidth(); int heigh ...
- CoordinatorLayout-带图片伸缩工具栏
伸缩工具栏toolbardesign 效果图: 步骤一: 在build.gilde中添加以下代码 dependencies { compile fileTree(dir: 'libs', includ ...
- ASP.NET MVC内置的Filter实现介绍
有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你创建action过滤器.Action过滤器是自定义的Attributes,用来标记添 ...