[leetcode.com]算法题目 - 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.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
class Solution {
public:
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n<) return;
int first = ;
int last = n-;
while(first<n && ==A[first])
first++;
while(last>= && ==A[last])
last--;
int i=first;
while(first < last && i<=last){
if (==A[i]){
swap(A[i],A[last]);
last--;
while(last>= && ==A[last])
last--;
}else if(==A[i]){
swap(A[i],A[first]);
first++;
if(i<first) i=first;
while(first<n && ==A[first])
first++;
}else{
i++;
}
}
}
};
我的答案
思路:如果只需要一次遍历,就地排序,其实在循环到每个数的时候要比两次遍历多做一些事情。因为0肯定是排在前面的,2肯定是排在后面的,指定两个指针,分别指向数组的两头,然后往中间缩进,注意分别讨论遍历到0,1,2时应该做何种运算,何种操作即可。
[leetcode.com]算法题目 - Sort Colors的更多相关文章
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- LeetCode(75) Sort Colors
题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same c ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- IO之4种字节流拷贝文件方式对比
public class CopyMp4Demo { public static void main(String[] args) throws IOException { long start = ...
- javabean为什么需要序列化
无论用hibernate或者mybatis结合spring做开发还是其他,系统里持久类往往要实现序列化, implements Serializable.我还是比较好奇,为什么要这样做呢?一直只知道个 ...
- 超高清视频会议所需带宽分析---1M带宽应用720P是否可能?
超高清视频会议所需带宽分析 ---1M带宽应用720P是否可能? 1.首先计算720P(1280×720)单幅图像照片的数据量 每像素用24比特表示,则:720P图像照片的原始数据量= 1280×72 ...
- 2018.11.02 洛谷P2312 解方程(数论)
传送门 直接做肯定会TLETLETLE. 于是考验乱搞能力的时候到了. 我们随便选几个质数来checkcheckcheck合法解,如果一个数无论怎么checkcheckcheck都是合法的那么就有很大 ...
- idea环境下js、css中文乱码
idea2018.2+tomcat8+java8+win10 异常:本地js和css通过tomcat发布时,在页面打印出的中文是乱码.而从数据库读取的中文数据和html的中文显示正常. 解决方法: 步 ...
- JAVA遇上HTML-----JSP 篇基本概念
Java Web简介 1.什么是WEB应用程序: Web应用程序是一种可以通过Web访问的应用程序.Web应用程序的一个最大好处是用户很容易访问应用程序.用户只需要有浏览器即可,不需要再安装其他软件. ...
- Echarts的使用方法
效果图: 1. 在echarts官网下载包,解压后,将文件Echarts\echarts-2.2.7\echarts-2.2.7\doc\example\www\js\echarts.js文件拷贝,放 ...
- s5-11 距离矢量路由选择协议
距离矢量路由选择(Distance Vector:DV) 每个路由器维护一张表,表中列出了当前已知的到每个目标 的最佳距离,以及为了到达那个目标,应该从哪个接口转发. 距离矢量路由选择(Distanc ...
- SpringBoot2.0.2 Application调用的三种方式
一.注解 @SpringBootApplication 点开查看源码是由多个注解合成的注解,其中主要的注解有: @SpringBootConfigurati ...
- BZOJ 1029 [JSOI2007]建筑抢修 (贪心 + 优先队列)
1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 5452 Solved: 2422[Submit][Statu ...