Sort Colors [LeetCode]
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?
Summary: Accepted at the first submission. Nice.
void sortColors(int A[], int n) {
int red_idx = -;
int blue_idx = n;
for(int i = ; i < n; i ++) {
if(i == blue_idx)
break;
if(A[i] == ){
int tmp = A[i];
A[i] = A[++red_idx];
A[red_idx] = tmp;
}
if(A[i] == ) {
int tmp = A[i];
A[i] = A[-- blue_idx];
A[blue_idx] = tmp;
i--;
}
}
}
Sort Colors [LeetCode]的更多相关文章
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- Sort Colors —— LeetCode
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Sort Colors leetcode java
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括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 ...
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
随机推荐
- 使用JDBC实现Oracle用户认证
两天时间写的小品,以前的J2EE环境基本使用框架.现在使用JDBC配合Oracle存储过程模拟了一下用户注册和用户认证. 一.添加必须的jar包 需要JDBC连接Oracle的包和shiro-core ...
- 关于sql的关联排序
个人觉得对sql的玩转其实是sql中各种函数方法的理解的透彻. 原始数据表 要求查询的结果排序 其实刚看题目可能觉得要用group by但是再网下继续思考可能就没有思路了 但是如果你接触过over的试 ...
- python-进程,线程,协程
1.进程和线程 进程定义:进程是正在运行的程序的实例,进程是内核分配资源的最基本的单元,而线程是内核执行的最基本单元,进程内可以包含多个线程,只要记住这三个要点,就可以很清楚的理清进程和线程的行为模 ...
- er
电子锁管理 设备管理 设备管理 信息编辑;回收 电子锁发放 电子锁初始化,发放 记录车辆在发车时使用的电子锁 电子锁开锁联系人管理 电子锁开锁联系人管理 根据订单路线中的投点,设置每个投递点的开锁联系 ...
- apache代理服务器为nodejs服务设置域名
本机以apache为主,其中 在httpd.conf中先设置 <VirtualHost *:80> ServerName nodejs.cc ServerAlias www.nodejs. ...
- TTradmin v1.1 - 免端口映射穿透任何内网、基于radmin核心的即时远程协助
TTradmin 是一款免端口映射可直接穿透任何内网,基于radmin核心的即时远程协助软件.在使用的时候只需要保证“协助端”和“被协助端”使用同一个验证码即可实现安全便捷的远程控制,不需要进 ...
- Linux启动过程详述
http://www.ibm.com/developerworks/cn/linux/kernel/startup/index.html Linux启动第1步:引导内核 Linux启动第2步:内核部分 ...
- 解决小米、红米及其他 Android 手机无法在 Mac 下进行真机调试的问题(转)
转自:http://ju.outofmemory.cn/entry/103522 Begin iOS 2014-08-19 271 阅读 手机 Android 小米 mac 调试 在 Mac OSX ...
- spring-事物-jdbc-hibernate
- 一位程序员如何修炼成CTO
几乎整个互联网行业都缺CTO,特别是一些草根背景的创业者,这个问题更加显著.从我自己的感受,身边各种朋友委托我找CTO的需求,嗯,算下来超过两位数了,光最近一个月就有3个,而且这三家都是刚拿了A轮的. ...