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 ...
随机推荐
- LeetCode 2 Add Two Numbers 模拟,读题 难度:0
https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...
- 基于内存,redis,mysql的高速游戏数据服务器设计架构
转载请注明出处,欢迎大家批评指正 1.数据服务器详细设计 数据服务器在设计上采用三个层次的数据同步,实现玩家数据的高速获取和修改. 数据层次上分为:内存数据,redis数据,mysql数据 设计目的: ...
- BestCoder Round #53 (div.1)
Problem A: 题目大意: 给出以节点1为根的一棵树A,判断它是否是特殊的.一棵树是特殊的当且仅当不存在和它不完全相同的一棵树B,使得A中点i到点1的距离和B中相等. 题解: 假设一个点x的深度 ...
- 锤子手机 Smartisan M1L 咖啡金 真皮背面 高配版 5.7
http://www.smartisan.com/m1/#/os 快人一步的OS http://www.smartisan.com/shop/#/buyphone?c=coffee&v= ...
- JS 数字,金额 用逗号 隔开(数字格式化)
<script> function fmoney(s,n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s ...
- 011-Scala中的apply实战详解
011-Scala中的apply实战详解 object中的apply方法 class中的apply方法 使用方法 apply方法可以应用在类或者Object对象中 class类 必须要创建实例化的类对 ...
- js⑦
立即执行函数or自执行函数 为了避免全局变量的产生.(function(){ //var a = 10; //var b = 20;//console.log(a,b); -------------v ...
- ios之JavaScript
初次接触java脚本,感觉java脚本so interesting!为什么呢?写javascript代码感觉就像是在记流水账,无拐弯抹角,一个字,就是"干",想怎么干就怎么干,哈哈 ...
- php 数据库备份、还原
1. mydb.php //DB类 2. backup.php //备份脚本 3. restore.php //还原脚本 mydb.php <? class db{ var $linkid; v ...
- JS在火狐浏览器下如何关闭标签?
首先,要确定火狐设置是否允许通过JS代码window.close()方法关闭标签. 确定方式如下: 在Firefox地址栏里输入 about:config 在配置列表中找到dom.allow_scri ...