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的更多相关文章

  1. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  2. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  3. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  4. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  5. 52. Sort Colors && Combinations

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  6. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  7. Sort Colors I & II

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. github上面建立分支

    首先是有一个github的账户,然后随便开个项目. 好了,现在把git命令行打开,输入下面几行代码. git clone https://github.com/user/repository.git ...

  2. Android的RecyclerView

    简介 RecyclerView是support-v7中用来替换ListView的组件.RecyclerView 小组件比 ListView 更高级且更具灵活性. 此小组件是一个用于显示庞大数据集的容器 ...

  3. 使用Volley执行网络数据传输

    首先需要实例化一个RequestQueue RequestQueue queue = Volley.newRequestQueue(this); 然后是根据提供的URL请求字符串响应 String u ...

  4. 重叠I/O模型

    一. 重叠I/O的概念当调用ReadFile和WriteFile时,如果最后一个参数lpOverlapped设置为NULL,那么线程就阻塞在这里,直到读写完指定的数据后,它们才返回.这样在读写大文件的 ...

  5. 例子:Backup Isolated Storage To SkyDrive

    本例演示了如何从隔离存储中备份文件到SkyDrive. 1. 登陆微软网站,申请“Create application”,获取一个“Client ID” 2. XAML中添加对Live相关控件库的命令 ...

  6. VMware下利用ubuntu13.04建立嵌入式开发环境之三

    系统环境建立完成后就要安装和配置嵌入式开始需要的工具和服务. 一般我们在交叉编译是需要的服务有:smb.tftp.telnet.nfs.ssh和x11等.下面一步步,介绍如何安装这些服务. 一.smb ...

  7. Linux 终端常用快捷键

    Tab 自动补全 Ctrl+L 清屏 Ctrl+A 移动到头 Ctrl+E 移动到尾 Ctrl+左键头 左移一个单词 Ctrl+右箭头 右移一个单词 Ctrl+U 删除左边所有字符 Ctrl+K 删除 ...

  8. 程序内部让用户直接上appstore评价游戏的链接地址以及跳转方法

    NSString *str = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore ...

  9. unity5.0材质添加问题

    将Material拖放到Cube的Inspector视图中 材质添加图片背景,unity自带了很多种材质类型点击Shader下拉按钮可以更换材质类型为Diffuse camera preview: G ...

  10. 中小网站如何使用谷歌Adsence国际化优势赚钱?

    劲捷电子信息有限公司是一家拿了美国风险投资的互联网公司.他的创办人祁劲松2005年加入AdSense,迄今收获颇丰,为此他还写了一本 <Google AdSense实战宝典>.在祁劲松看来 ...