Sort colors系列
75. Sort Colors
问题描述:
给一个包含n个数字的数组,其中有0,1,2;排序使得所有相同的数字相邻,且按照0,1,2的顺序。
思路:
(1)计数排序:
需要扫两遍数组,一遍统计个数,第二遍开始摆放数字。
代码如下:
void sortColors(vector<int>& nums) {
int i=,j=,k=;
int n=nums.size();
for(int p=;p<n;p++)
{
if(nums[p]==)
i++;
else if(nums[p]==)
j++;
else
k++;
}
for(int p=;p<n;p++)
{
if(p<i)
nums[p]=;
else if(p>=i&& p<i+j)
nums[p]=;
else
nums[p]=;
}
}
(2)如果只能扫一遍,很容易想到的就是左边存放0和1,右边存放2.两边往中间靠。
设置两个index,left记录第一个1的位置,left左边为0,right记录第一个非2的位置,right右边为2.
然后使用i从头到尾扫一遍,直到与right相遇。
i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。
需要注意的是:由于left记录第一个1的位置,因此A[left]与A[i]交换后,A[left]为0,A[i]为1,因此i++;
而right记录第一个非2的位置,可能为0或1,因此A[right]与A[i]交换后,A[right]为2,A[i]为0或1,i不能前进,要后续判断。
由此该数组分为4段:[0,left)-->0; [left,i)-->1; [i,right]-->乱序; (right,n-1]-->2
0 0 0 1 1 1 2 1 0 2 1 2 2 2
^ ^ ^
left i right
最直接的方法就是partition方法,代码如下:
void sortColors(int A[], int n) {
int left = ;
int right = n-;
int i = ;
while(i <= right)
{
if(A[i] == )
{
swap(A[left], A[i]);
left ++;
i ++;
}
else if(A[i] == )
{
i ++;
}
else
{
swap(A[i], A[right]);
right --;
}
}
}
(3)最漂亮的一种做法:
【最直观:平移插入】
void sortColors(int A[], int n) {
int i = -;
int j = -;
int k = -;
for(int p = ; p < n; p ++)
{
//根据第i个数字,挪动0~i-1串。
if(A[p] == )
{
A[++k] = ; //2往后挪
A[++j] = ; //1往后挪
A[++i] = ; //0往后挪
}
else if(A[p] == )
{
A[++k] = ;
A[++j] = ;
}
else
A[++k] = ;
} }
Sort colors系列的更多相关文章
- 【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 ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 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: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 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 ...
随机推荐
- 学习笔记(二):使用 TensorFlow 的起始步骤(First Steps with TensorFlow)
目录 1.工具包 TensorFlow 张量 (Tensor) 图 (graph) TensorBoard 2.tf.estimator API Estimator 预创建的 Estimator (p ...
- 在Keras中导入测试数据的方法
https://blog.csdn.net/ethantequila/article/details/80322425?utm_source=blogxgwz2
- 深入理解 hashcode 和 hash 算法
深入理解 hashcode 和 hash 算法 2017年12月30日 23:06:07 阅读数:5197 标签: hashhashmaphashcode二进制 更多 个人分类: jdk-源码 ht ...
- 201621123080 《Java程序设计》第10周学习总结
201621123080 <Java程序设计>第10周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- 如何使用jmeter做接口测试
1.传参:key=value形式 2.传参:json格式 3.jmeter上传文件 4.jmeter传cookie 或者使用 HTTP Cookie管理器
- word域代码判断奇偶插入分页符
阿拉伯数字页码判断奇偶插入分页符(PAGE表示当前页码,QUOTE 12表示插入分页符) {IF{=MOD({PAGE},2)} = 1 "{ QUOTE 12}" " ...
- php使用curl访问https返回无结果的问题
最近在做一个微信自动登录,发起验证以后回调页面获取openid时 curl函数返回空. $appid = "appid appid "; $secret = "secre ...
- LeetCode(234) Palindrome Linked List
题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...