LeetCode OJ: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.
将数组中的0,1,2分别排序,使得相同的数字相邻,这个比较简单,直接看代码:
class Solution {
public:
void sortColors(vector<int>& nums) {
int redNum = count(nums.begin(), nums.end(), );
int whiteNum = count(nums.begin(), nums.end(), );
int blueNum = count(nums.begin(), nums.end(), );
int sz = nums.size();
vector<int> ret(sz, );
int redCount = , whiteCount = , blueCount = ;
for(int i = ; i < sz; ++i){
if(nums[i] == ){
ret[redCount++] = ;
}else if(nums[i] == ){
ret[whiteCount + redNum] = ;
whiteCount++;
}else{
ret[blueCount + redNum + whiteNum] = ;
blueCount++;
}
}
nums = ret;
}
};
当时题目要求的事one-pass,const-space algorithm,当时硬是没想出来,现在看了下别人的解答,真简洁,原理很简单,3指针,画个图就能看懂了,代码如下所示:
public class Solution {
public void sortColors(int[] nums) {
int i = 0;
int j = nums.length - 1;
int k = nums.length - 1;
while(i <= j){
if(nums[i] == 2){
int tmp = nums[k];
nums[k] = nums[i];
nums[i] = tmp;
k--;
if(k < j)
j = k;
}else if(nums[i] == 1){
int tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;
j--;
}else{
i++;
}
}
}
}
LeetCode OJ:Sort Colors(排序颜色)的更多相关文章
- 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(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- [LeetCode OJ] Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- [LeetCode] Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [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 (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- sqlserver整理的实用资料
1 --- 创建 备份数据的 device 2 3 USE DB_ZJ 4 EXEC sp_addumpdevice 'disk', 'testBack', 'c:\MyNwind_1.dat' 5 ...
- OVF and OVA
最近测试的东西有关于ovf 和ova等相关用例,在网上找了点内容摘抄了下来. 一.什么是OVF文件 开源虚拟化格式OVF文件是一种开源的文件规范,它描述了一个开源.安全.有效.可拓展的便携式虚拟打包以 ...
- mysql数据库补充知识7 索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- django自带的用户认证和form表单功能
一.用户认证 1.用户认证方法 1.ajango自带用户认证功能,只需要引入相应的模块就可以使用,但是前提是必须使用ajango自带的auth_user表,并且需要把用户相关信息存放在该表中. 2.引 ...
- vim插件快捷键
@1:winmanager: #1:打开winmanager的快捷键在.vimrc中配置,默认为":WMToggle",使用nmap可以将其映射到其他的命令. #2:netrw快捷 ...
- mysql只能连接localhost解决
grant all privileges on *.* to 'root'@'%' identified by 'root';flush privileges;
- c# 抽象类(abstract)
using System; using System.Collections.Generic; using System.Linq; using System.Text; //抽象类(abstract ...
- Python学习进程(6)函数
函数最重要的目的是方便我们重复使用相同的一段程序. (1)函数的定义: 函数定义的简单规则: 1.函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(): 2.任何传入参数和 ...
- [POI2008]砖块Klo
题目 爆炸\(OJ\)机子太慢了吧实在不想打平衡树了 做法 烂大街的一个概念:求中位数 然后求前缀差和后缀差,主席树模板题 注意\(int\)和\(long long\) My complete co ...
- P4755 Beautiful Pair
题目 洛谷 做法 \(i≤x≤j,a[i]<\frac{a[x]}{a[j]}\) 考虑\(a[x]\)的贡献,单调栈预处理\(L,R\)能作为最大值的区间 枚举一端点,仅需另一端点满足条件即可 ...