- 浏览: 172721 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
Note:
You are not suppose to use the library's sort function for this problem.
解决这道题目我们可以采用计数排序算法。首先我们遍历数组得到每个颜色的个数,记录到计数数组count[i]中;接下来我们累加计数数组,使count[i] = count[i] + count[i - 1],这样其实就是给不同的元素给定了不同大小的空间;然后我们遍历需要排序的数组,用一个临时数组tem[]记录遍历过的元素,使tem[--count[nums[i]]] = nums[i];这样相当于将每种颜色从它所属的空间后面开始填充,直到结束。代码如下:
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
解决这道题目我们可以采用计数排序算法。首先我们遍历数组得到每个颜色的个数,记录到计数数组count[i]中;接下来我们累加计数数组,使count[i] = count[i] + count[i - 1],这样其实就是给不同的元素给定了不同大小的空间;然后我们遍历需要排序的数组,用一个临时数组tem[]记录遍历过的元素,使tem[--count[nums[i]]] = nums[i];这样相当于将每种颜色从它所属的空间后面开始填充,直到结束。代码如下:
public class Solution { public void sortColors(int[] nums) { if(nums == null || nums.length == 0) return ; int[] count = new int[3]; int[] tem = new int[nums.length]; for(int i = 0; i < nums.length; i++) count[nums[i]] ++; for(int i = 1; i < 3; i++) count[i] = count[i] + count[i - 1]; for(int i = 0; i < nums.length; i++) tem[--count[nums[i]]] = nums[i]; System.arraycopy(tem, 0, nums, 0, tem.length); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 223Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 222You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 338Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 329Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 447Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 426Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 424The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 382Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 513Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 526Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 877Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 551Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 595Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 760Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 728You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 625For a undirected graph with tre ...
相关推荐
var sc = new SortColors ( colors ) ; sc . shuffle ( ) ; var list = sc . get ( ) ; 支持的格式: 十六进制 RGB(即将推出) HSV(很快) HSL(很快) 可用方法: add(color) - 为集合添加颜色 set(colors) -...
素描插件排序背景颜色只是一个简单的插件,可通过其背景色相值对所选画板(或所...background-colors.sketchplugin文件以安装插件完成了 :party_popper: 您可以在“ Plugins > Sort Colors > Sort by Hue下找到该插件。
| _O(n)_ ~ _O(n^2)_ | _O(n)_ | Medium || Bit Manipulation, Counting Sort, Pruning| 342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](./C++/power-of-four.cpp) [Python](./...
Sort Colors Kth Largest Element in an Array 桶排序 First Missing Positive 计数排序 H-Index 基数排序 Maximum Gap 其他 Largest Number 小结 查找 Search for a Range Search Insert Position Search in ...
leetcode中国 每周小测 每周题目 week 1 adjust : 将数组中指定索引处的值替换为经函数变换的值 实现版本: ramda版本参考: groupAnagrams ...给定一个字符串数组,将字母异位词组合在一起。...sortColors
示例:输入: [2,0,2,1,1,0]输出: [0,0,1,1,2,2]算法1:由于元素是固定的,可以采用计数的方法def sortColors(self,
Colors Leetcode 215. Kth Largest Element Leetcode 4. Median of Two Sorted Arrays 注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考 链表类(Linked List): 基础知识:链表如何...
_75_SortColors_twopointer _142_LinkedListCycle2_twopointer //////// 弗洛伊德循环检测 _287_FindtheDuplicateNumber_TwoPointer_Floyd _340_LongestSubstringwithAtMostKDistinctCharacters_TwoPointer _424_...
样品分类颜色 使用JavaScript将多种随机颜色分类为彩虹色的示例。 $ git clone https://github.com/tsuyoshiwada/sample-sort-colors.git $ cd sample-sort-colors $ npm install $ gulp
Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring...
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone ...Sort Colors M
sortcolors: 对三种颜色的方块排序 water: 不同高度的台阶,能够蓄水多少 quicksort: 面试必考之一,快速排序 heapsort: 堆排序算法 B+tree: B+树 RedBlackTree:红黑树 AVLTree: 自平衡的二叉查找数 Dijkstra:最短...
扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 maxArea 26.删除排序数组中的重复项 removeDuplicates 33.搜索旋转排序数组 ...sortColors 179.最大数 largestNumber 324.摆
leetcode中国 leetcode-study 地址: 考虑维度 时间维度:是指执行当前算法所消耗的时间,我们通常用「时间复杂度」来描述。 空间维度:是指执行当前...SortColors 贪心思想 给小孩分配饼干,最多分配多少个 AssignCook
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆...Colors 计数排序 | | | 88 Merge So
SortColors 167 - TwoSum2 DCP 75 是一个双指针问题,如果当前项为 0,则使用 p1 p2 指向开始和结束,然后与开始交换,如果当前项目为 2,则与结束交换。 167是同一个想法 02/01/2020 16 -3SumClosest 344 - Reverse...
leetcode卡Leetcode-解决方案 LeetCode DS 日常挑战的解决方案 问题陈述 1.InvertBinaryTree - 2.子序列- 3.SearchInsertPosition - 4.SortColors - 5.单号——
$> colors (colorScale HSL hotpink Nil darksalmon) 5 " #ff69b4 " : " #fa6d99 " : " #f47182 " : " #ef7d76 " : " #e9967a " : Nil > toHexString <$> (sortBy (comparing luminance) [black, white, blue, ...
sort ( colorSort . sortFn )// Or: // sorted = colorSort(colors)// => sorted:// [// 'red',// 'hsl(0, 10%, 60%)',// '#000'// ]例子这些示例可以在上看到,该程序包用于对颜色进行排序。CSS技巧粉碎杂志 引导...
I asked my friend to write it adding some event handling (colors, on over, etc) and a simple algorithm to check serial. He also wrote the proggy using more source files and making various subs (some ...