LeetCode——Sort Colors
来源:互联网 发布:wemall商业版java源码 编辑:程序博客网 时间:2024/05/17 01:33
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.
Java代码public class Solution { public void sortColors(int[] A) { int m =0; int n =0; int k =0; for(int i =0;i<A.length;i++) if(A[i]==0) m++; else if(A[i]==1) n++; else k++; for(int i=0;i<A.length;i++) A[i] = i<m?0:i<(m+n)?1:2; }}
上述代码通过。
题目中说,能否用一趟的算法,常数的存储空间实现?马上想到的是快排的左右分堆的方法,,,试了好久没试出来。。以后再想
0 0
- Sort Colors —LeetCode
- LeetCode —— Sort Colors
- LeetCode——Sort Colors
- LeetCode——Sort Colors
- Sort Colors —1—Leetcode
- leetcode 075 —— Sort Colors
- Leetcode题集——sort-colors
- Leetcode——75. Sort Colors
- LeetCode 之 Sort Colors — C 实现
- leetcode——75——Sort Colors
- Leetcode刷题记——75. Sort Colors(颜色排序)
- LeetCode刷题(C++)——Sort Colors(Medium)
- LeetCode: Sort Colors
- LeetCode Sort Colors
- LeetCode : Sort Colors
- leetcode 77: Sort Colors
- Leetcode 75 Sort Colors
- leetcode 101: Sort Colors
- Codeforces Round #276 (Div. 2)(C)
- select元素的操作使用类
- [BZOJ 1056][BZOJ 1862][HAOI 2008][ZJOI 2006]GameZ游戏排名系统(排名系统)(Treap+哈希表)
- 数据结构概述 第五章 二叉树
- 自定义记录格式输出HDFS文件
- LeetCode——Sort Colors
- bzoj 1502: [NOI2005]月下柠檬树
- Spring源码--Spring core中对Future的扩展
- API - jQuery之操作cookie
- XML概述
- 自定义属性TypedArray
- 【VS2013】windows多线程编程_并行评测
- js中encodeURI与python中urllib.quote对应
- HLJUOJ1144(Dijkstra模板应用)