【leetcode】Sort Colors(middle)☆
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(int A[], int n) {
MergeSort(A, , n - );
}
void MergeSort(int A[], int l, int r)
{
if(l < r)
{
int mid = (l + r) / ;
MergeSort(A, l, mid);
MergeSort(A, mid + , r);
Merge(A, l, mid, r);
}
}
void Merge(int A[], int l, int mid, int r)
{
int x = mid - l + ;
int y = r - mid;
int * B = new int[x];
int * C = new int[y];
memcpy(B, A + l, x * sizeof(int));
memcpy(C, A + mid + , y * sizeof(int));
int i = , j = , k = l;
while(i < x && j < y)
{
if(B[i] < C[j])
A[k] = B[i++];
else
A[k] = C[j++];
k++;
}
if(i >= x)
memcpy(A + k, C + j, (y - j) * sizeof(int));
else
memcpy(A + k, B + i, (x - i) * sizeof(int));
delete [] B;
delete [] C;
}
看看大神的线性时间、常量空间的方法
①说实话,我一眼望过去看不懂啊。研究了半天,发现 n0是最后一个0个位置, n1是最后一个1的位置, n2是最后一个2的位置
每次新来一个0,需要依次把2、1向后延1位。
就是2先向后多占一个位置,然后1向后多占一个位置,把2最前面的给覆盖,0再多占一个位置,新加的0覆盖了1最前面多出来的。
依此类推。
void sortColors2(int A[], int n) {
int n0 = -, n1 = -, n2 = -;
for (int i = ; i < n; ++i) {
if (A[i] == )
{
A[++n2] = ;
A[++n1] = ;
A[++n0] = ;
}
else if (A[i] == )
{
A[++n2] = ;
A[++n1] = ;
}
else if (A[i] == )
{
A[++n2] = ;
}
}
【leetcode】Sort Colors(middle)☆的更多相关文章
- 【LeetCode】Sort Colors 解题报告
[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【leetcode】Sort List (middle)
Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...
- 【Leetcode】Sort List JAVA实现
Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...
- 【LeetCode】 sort list 单清单归并
称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...
- 【数组】Sort Colors
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Sort List
Sort List Sort a linked list in O(n log n) time using constant space complexity. 需要采用归并排序对链表进行操作. ...
随机推荐
- 江湖救急:webbrowser中js文件丢失问题~
页面中,有一个按钮,点击按钮通过js create 了一个 script标签 ,链接加载一个外部js文件,执行该js文件 $("#a").click(function(){ $.g ...
- 系统研究Airbnb开源项目airflow
开源项目airflow的一点研究 调研了一些几个调度系统, airflow 更满意一些. 花了些时间写了这个博文, 这应该是国内技术圈中最早系统性研究airflow的文章了. 转载请注明出处 htt ...
- Cotex-M3内核LPC17xx系列时钟及其配置方法
一.背景: 最近正在接手一个项目,核心芯片既是LPC17XX系列MCU,内核为ARM的Cotex-M3内核. 想要玩转一个MCU,就一定得搞定其时钟! 时钟对MCU而言,就好比人类的心脏.由其给AHB ...
- tcl实现http请求
package require "http" proc errLog args { puts $args } proc SendHttp args { global token s ...
- Moment.js 超棒Javascript日期处理类库
Moment.js 不容错过的超棒Javascript日期处理类库 主要特性: 3.2kb超轻量级 独立类库,意味这你不需要倒入一堆js 日期处理支持UNIX 时间戳,String,指定格式的Date ...
- 使用C++扩展Python的功能 转自:http://blog.csdn.net/magictong/article/details/8897568#comments
使用C++扩展Python的功能 环境 VS2005Python2.5.4 Windows7(32位) 简介 长话短说,这里说的扩展Python功能与直接用其它语言写一个动态链接库,然后让Python ...
- Visual Studio error C2001:常量中有换行符(解决办法)
在Visual Studio自动生成的项目中,碰见了一件关于文件编码的问题,集中在类似于以下的语句上: DASLog (DASProtWarn, L"(%s)消息超时,进入慢循环召唤模式.& ...
- 跟着百度学PHP[4]OOP面对对象编程-6-封装性private
所谓封装顾名思义,如同箱子般给封装起来.结合前面的来说就是对属性或者方法,封装后的方法或属性只能有类内部进行调用.外部调用不了. 封装性的好处: 1.信息隐藏 2.http://www.cnblogs ...
- cvGet2D的用法
CvScalar s;s = cvGet2D(src, j,i);//获取src图像中坐标为(i,j)的像素点的值s.val[0] 代表src图像BGR中的B通道的值~int nXY = cvGet2 ...
- BZOJ 3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
Description 给你每个点与相邻点的距离和方向,求两点间的曼哈顿距离. \(n \leqslant 4\times 10^4\) . Sol 加权并查集. 像向量合成一样合并就可以了,找 \( ...