leetCode(30):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.
void sortColors(vector<int>& nums)
{//排序时间复杂度O(n)
int length=nums.size();
int zero=0;
int two=length-1;
while(zero<length && nums[zero]==0)
{//找到第一个不为0的数的下标
zero++;
}
while(two>=zero && nums[two]==2)
{//找到倒数第一个不为2的数的下标,zero前的数都是0
two--;
} for(int i=zero;i<=two;++i)
{
if(nums[i]==0)
{//假设为0,和zero指向的数交换,zero前移
nums[i]=1;//zero指向的值肯定是1,首先不可能为0,其次,i遍历过的地方,2都已经换到最后
nums[zero]=0;
zero++;
}
else if(nums[i]==1)
{//假设为1,则跳过
continue;
}
else
{//假设为2。则和two指向的数交换,并更新two的值。
//和zero不同,two前面的数可能还未排序
nums[i]=nums[two];
nums[two]=2;
two--;
while(two>=i && nums[two]==2)
{
two--;
}
i--;//先向后退一步。由于two指向的数还未进行排序
}
}
}
leetCode(30):Sort Colors的更多相关文章
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- 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】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题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [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 ...
- 【leetcode】Sort Colors(middle)☆
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- ThreadLocal,静态变量,实例变量,局部变量的线程安全
之前都是业务层次开发,现在公司进行的网络编程,一下子要了解太多java底层的东西并进行应用,我现在边学习边应用.由于知识能力有限,在上次发博客时出现了一个小小的纰漏,而这个纰漏被细心的博友发现了. 首 ...
- Python描述符:property()函数的小秘密
描述符:将某种特殊类型的类的实例指派给另一个类的属性(注意:这里是类属性,而不是对象属性).而这种特殊类型的类就是实现了__get__,__set__,__delete__这三个方法中的一个或多个的新 ...
- Oracle调整内存参后报ORA-00844和ORA-00851
数据库服务器内存由16G增加为64G,为充分利用内存资源,对Oracle内存参数做了如下调整: SQL>alter system set sga_max_size=40960M scope=sp ...
- android 国际化 横屏(land) 竖屏(port)margin外边距和padding内边距
android 国际化 横屏(land) 竖屏(port) 边距又分为内边距和外边距,即margin和padding.
- 努比亚 Z17 mini s (Nubia NX589J) 解锁BootLoader 并刷入recovery ROOT
首先下载好工具链接:链接:https://pan.baidu.com/s/1gher4T9 密码:rypn 备用下载链接:https://pan.baidu.com/s/1nxdzt9Z 本篇教程教你 ...
- HDU_1969_二分
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- C# 截取字符串基本
#region --构建字符串处理 string str1 = "123AAA456AAAA789AAAAAAA1011"; string str2 = "1234567 ...
- java 循环document 通用替换某个字符串或特殊字符
document 生成xml时 报错 XML-20100: (Fatal Error) Expected ';'. 查了半天发现是 特殊字符 & 不能直接转出,需要进行转换,因为是通用方法很 ...
- 深度遍历DFS---树
一.二叉树的深度 题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nul ...
- Ubuntu的shell执行过程
登录shell(login shell)会执行.bash_profile,.bash_profile中会执行.profile,.profile中会执行.bashrc 非登录shell(non-logi ...