【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. 需要采用归并排序对链表进行操作. ...
随机推荐
- Error: [ng:areq] Argument 'xxxx' is not a function, got undefined
"Error: [ng:areq] Argument 'keywords' is not a function, got undefined" 代码类似这样的: <div n ...
- MongoDB MapReduce学习笔记
http://cnodejs.org/topic/51a8a9ed555d34c67831fb8b http://garyli.iteye.com/blog/2079158 MapReduce应该算是 ...
- 【bzoj1036】[ZJOI2008]树的统计Count
题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v ...
- 【bzoj4027】[HEOI2015]兔子与樱花
题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...
- PHP的$_SERVER['PHP_SELF']造成的XSS漏洞攻击及其解决方案
$_SERVER['PHP_SELF']简介 $_SERVER['PHP_SELF'] 表示当前 PHP文件相对于网站根目录的位置地址,与 document root 相关. 假设我们有如下网址,$_ ...
- View和ViewImage设置图片
1.view类的设置背景android:background --setBackgroundResource(int) --A drawable to use as the background. s ...
- Codeforce 287 div2 C题
题目链接:http://codeforces.com/contest/507/problem/C 解题报告:现在有一个满二叉树型的迷宫,入口在根结点,出口在第n个叶节点,有一串命令,LRLRLRLRL ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- C++ 模板函数与模板类
一.模板函数 函数模板提供了一类函数的抽象,即代表了一类函数.当函数模板被实例化后,它会生成具体的模板函数.例如下面便是一个函数模板:
- Oracle 管道化表函数
在PL/SQL中,如果要返回数据的多个行,必须通过返回一个REF CURSOR的游标,或者一个数据集合(如临时表或物理表)来完成,而REF CURSOR的局限于可以从查询中选择的数据,而数据集合的局限 ...