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 ...
随机推荐
- CentOS6下搭建OpenVPN服务器
• OpenVPN简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.使用OpenVPN可以方便地在家庭.办公场所.住宿酒店等不同网 ...
- 7. Reverse Integer java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 代码如下: pub ...
- Testing Round #8 A. IQ Test 水题
题目链接:http://codeforces.com/problemset/problem/328/A 这道题目wa了一次,注意这句话: You should also print 42 if the ...
- DataTransfer(setData()方法)
DataTransfer对象专门用来存储拖放时要携带的数据,它可以被设置为拖放事件对象的DataTransfer属性.---把拖动的数据存入其中setData有两个参数:1.第一个参数为携带数据的数据 ...
- leetcode 118 Pascal's Triangle ----- java
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 输入DStream和Receiver详解
输入DStream代表了来自数据源的输入数据流.在之前的wordcount例子中,lines就是一个输入DStream(JavaReceiverInputDStream),代表了从netcat(nc) ...
- [POI 2008][BZOJ 1132]Tro
这题我真是无能为力了 这题的做法还是挺简单的 枚举左下角的点做为原点,把其余点按极角排序 PS.是作为原点,如枚举到 k 时,对于所有 p[i] (包括p[k]) p[i]-=p[k] (此处为 ...
- IAR MSP430如何生成烧写文件
IAR生成430烧写方法有2种, 第一种是:将工程的debug模式切换成release模式,看图片操作. 那个.d43文件就是仿真调试模式的文件. 这里的test.txt文件就是烧写文件了,不要 ...
- caffe: compile error : undefined reference to `cv::imread(cv::String const&, int)' et al.
when I compile caffe file : .build_debug/lib/libcaffe.so: undefined reference to `cv::imread(cv::Str ...
- Threadpool dump
Microsoft (R) Windows Debugger Version 10.0.10586.567 AMD64Copyright (c) Microsoft Corporation. All ...