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 ...
随机推荐
- Maven学习笔记—坐标和依赖
Maven的坐标和依赖 1 Maven坐标 1.1 什么是Maven坐标 Maven坐标:世界上任何一组构件都可以使用Maven坐标来唯一标识,Maven坐标的元素包括groupId.artifact ...
- 解释一下python中的继承
当一个类继承另一个类,它就被称为一个子类/派生类,继承父类/基类/超类.它会继承/获取所有类成员(属性和方法) 继承能让我们重新使用代码,也能更容易的创建和维护应用 单继承:一个类继承单个基类 多继承 ...
- 【转】Python爬虫(6)_scrapy框架
官网链接:https://docs.scrapy.org/en/latest/topics/architecture.html 性能相关 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下 ...
- 值得关注的10个Python语言学习博客
大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建立这个博客的原因,向大家分享我自己学到的新知识.今天我向大家推荐10个值得我们关注 ...
- loadrunder之脚本篇——action分类
Action分类 l . Vuser_init 2. Vuser_end 3. Action 在lr中用户的初始化操作应该存放在Vuser_init中.用户的结束操作存放在Vuser_end中.因为 ...
- C# 学习黑马.Net视频教程,大文件拷贝
设计器代码: namespace 大文件拷贝 { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </summary> ...
- 大数据架构之:Spark
Spark是UC Berkeley AMP 实验室基于map reduce算法实现的分布式计算框架,输出和结果保存在内存中,不需要频繁读写HDFS,数据处理效率更高Spark适用于近线或准实时.数据挖 ...
- UVA11297 Census
题目 UVA11297 Census 做法 二维线段树,单点修改,矩阵查询,树套树(\(x,y\)),维护最大值最小值废话 有一点要注意的是:\(x\)树传到\(y\)树里面修改的时候,如果\(x\) ...
- 封装一个既能遍历数组又能遍历对象的的forEach函数
function newforEach(obj,fn) { var key; if(obj instanceof Array){ obj.forEach(function(item,index){ f ...
- CentOS 7 导入epel库
yum install epel-release 或者到百度云下载相应的 rpm 包进行安装 rpm -ivh epel-release-7-9.noarch.rpm