LintCode Sort Colors


For this problem we need to sort the array into three parts namely with three numbers standing for three different colors. Currently, the method in mind could be statistically get all the frequency of three numbers by screening the whole list. However, the chanllenge is to do it in O(N) time with extra O(1) storage. So, we want to use three pointers.
The two pointers used named as seperators to seperate the number which is less than 1 and larger than 1. When encountered the number less than 0 pl and i both increase. Because pl is pointing to the first element which is not 0.
class Solution {
/**
* @param nums: A list of integer which is 0, 1 or 2
* @return: nothing
*/
public void sortColors(int[] a) {
int pl = ;
int pr = a.length - ;
int i = ;
while (i <= pr) {
//pl could be view as the first index which is not 0
//every time after judge of two numbers i++ and pl++ if it is
//not 0
if (a[i] == ) {
swap(a,pl,i);
pl++;
i++;
}
else if (a[i] == ) {
i++;
}
//pr could be viwe as the first index which is not 2
else {
swap(a,pr,i);
pr--;
}
}
}
public void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
LintCode Sort Colors的更多相关文章
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 52. Sort Colors && Combinations
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【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解题报告—— 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 ...
随机推荐
- JS-DOM基础
1 JS-DOM 全称:document object model 1.1 获取页面元素 getElementsByTagName():无论元素的数量是多少,都会存入数组 getElement ...
- HDU--杭电--1026--Ignatius and the Princess I--广搜--直接暴力0MS,优先队列的一边站
别人都是广搜+优先队列,我没空临时学,所以就直接自己暴力了 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) ...
- java 配置环境变量
使用java编程首先需要安装jdk,然后还需要给你的电脑配置环境变量,下面就用图文演示如何配置环境变量: 1.右键我的电脑 -> 属性 2.点击“高级系统设置” 3.点击“环境变量” 4.在系统 ...
- ssh 注解写法
弄了半天 (好久哦) 首先 applicationContext-db.xml <?xml version="1.0" encoding="UTF-8" ...
- IOS UIButton用法详解
这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用. //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...
- android 开发中 添加库文件 和so 文件的存放位置和添加依赖
so文件一般存储在 main 当中 jniLibs 当中 然后在build.gradle中添加 sourceSets { main { jniLibs.srcDirs = ['src/main/j ...
- 初探canvas
canvas是html5新增的一个专用于图形处理的标签,利用canvas可以实现大部分图形操作canvas的一些基本操作与其他图形编程工具类似,包含:各种形状的边框.路径绘制和填充,画布属性调整,样式 ...
- 整合了一个功能强大完善的OA系统源码,php全开源 界面漂亮美观
整合了一个功能强大完善的OA系统源码,php全开源界面漂亮美观.需要的同学联系Q:930948049
- javascript运动应用
多物体运动框架: 1.多个盒子同时运动:move(obj,target)多一个参数 <!DOCTYPE html><html><head> <title> ...
- MySQL语句中的转义字符----引号
MySQL语言中的转义字符和各种编程语言基本相同,见下表 形式 含义 \0 0(NUL)字符 \n 换行 \r 回车符 \t 制表符 \b 退格 \' 单引号 \" 双引号 \\ 反斜线 \ ...