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 ...
随机推荐
- Python报错——module 'scipy.misc' has no attribute 'imresize'
报错是因为要安装PIL库,库名现在为Pillow,在命令行上安装即可: pip3 install Pillow
- 20170330 ABAP代理生成
在线文档ABAP代理生成, ABAP 代理生成(事物SPROXY)使您能够通过使用企业服务资源中企业服务资源库中 的接口描述在SAP系统中生成ABAP代理对象. ABAP代理生成概览: 1.代理标识, ...
- (扫盲)DTO数据传输对象
DTO即数据传输对象.但从定义上看就是简单的用来传递数据的.主要用途是在框架中定义DTO来绑定表现层中的数据.学过MVC.EF实体模型的都应该知道,我们可以定义一个Model实体来实现前后台数据的交互 ...
- socket()模块和套接字对象的内建方法
一.socket()模块函数 要使用socket.socket()函数来创建套接字,其语法如下: socket(socket_family,socket_type,protocol=0) 如上所述,s ...
- 最小化CentOS6.7(64bit)---安装mysql5.5、jdk、tomcat
********mysql******** ------------------------------------------------------------------------------ ...
- PAT 天梯赛 L1-027. 出租 【模拟】
题目链接 https://www.patest.cn/contests/gplt/L1-027 题意 给出一串电话号码,找出其中不同数字的个数,并且按递减顺序排列,然后 有一个index 数组,指出每 ...
- 【后缀数组之SA数组】【真难懂啊】
基本上一搜后缀数组网上的模板都是<后缀数组——处理字符串的有力工具>这一篇的注释,O(nlogn)的复杂度确实很强大,但对于初次接触(比如窝)的人来说理解起来也着实有些困难(比如窝就活活好 ...
- 如何单用户模式破解root密码&救援模式破解root密码
学了几天Linux,终于到了装逼时刻,看看如何破译别人的root密码,哈哈哈哈..... 单用户模式破解root密码 重置Centos 7 Root密码的方式 step1 - 在启动grub菜单,选择 ...
- PHP 获取真实IP地址
function getClientIp($type = 0) { $type = $type ? 1 : 0; static $ip = NULL; if ($ip !== NULL) return ...
- iOS_数据存取(二)
本节内容目录: 一.SQLite3 二.Core Data 一.SQlite3 SQLite3是⼀款开源的嵌入式关系型数据库,可移植性好.易使用.内存开销小SQLite3是⽆类型的,意味着你可以保存任 ...