`

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.
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);
    }
}
分享到:
评论

相关推荐

    SortColors:允许您对一系列颜色进行排序,从深到浅并反转

    var sc = new SortColors ( colors ) ; sc . shuffle ( ) ; var list = sc . get ( ) ; 支持的格式: 十六进制 RGB(即将推出) HSV(很快) HSL(很快) 可用方法: add(color) - 为集合添加颜色 set(colors) -...

    sketch-plugin-sort-background-colors:草绘插件,可根据图层背景填充颜色的色相值对图层进行排序

    素描插件排序背景颜色只是一个简单的插件,可通过其背景色相值对所选画板(或所...background-colors.sketchplugin文件以安装插件完成了 :party_popper: 您可以在“ Plugins &gt; Sort Colors &gt; Sort by Hue下找到该插件。

    LeetCode最全代码

    | _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](./...

    cpp-算法精粹

    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中国-quiz:每周小测

    leetcode中国 每周小测 每周题目 week 1 adjust : 将数组中指定索引处的值替换为经函数变换的值 实现版本: ramda版本参考: groupAnagrams ...给定一个字符串数组,将字母异位词组合在一起。...sortColors

    chenwc07#notebook#颜色分类1

    示例:输入: [2,0,2,1,1,0]输出: [0,0,1,1,2,2]算法1:由于元素是固定的,可以采用计数的方法def sortColors(self,

    lrucacheleetcode-LeetCodeSheet:记录自己Leetcode之旅

    Colors Leetcode 215. Kth Largest Element Leetcode 4. Median of Two Sorted Arrays 注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考 链表类(Linked List): 基础知识:链表如何...

    多线程leetcode-LeetCode:某物

    _75_SortColors_twopointer _142_LinkedListCycle2_twopointer //////// 弗洛伊德循环检测 _287_FindtheDuplicateNumber_TwoPointer_Floyd _340_LongestSubstringwithAtMostKDistinctCharacters_TwoPointer _424_...

    sample-sort-colors

    样品分类颜色 使用JavaScript将多种随机颜色分类为彩虹色的示例。 $ git clone https://github.com/tsuyoshiwada/sample-sort-colors.git $ cd sample-sort-colors $ npm install $ gulp

    javalruleetcode-LeetCode:LeetCode算法问题

    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:我自己的LeetCode解决方案

    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

    leetcode招聘-algorithm:算法

    sortcolors: 对三种颜色的方块排序 water: 不同高度的台阶,能够蓄水多少 quicksort: 面试必考之一,快速排序 heapsort: 堆排序算法 B+tree: B+树 RedBlackTree:红黑树 AVLTree: 自平衡的二叉查找数 Dijkstra:最短...

    扩展矩阵leetcode-Leetcode:LeetcodeAnswer-Java

    扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 maxArea 26.删除排序数组中的重复项 removeDuplicates 33.搜索旋转排序数组 ...sortColors 179.最大数 largestNumber 324.摆

    leetcode中国-leetcode-study:学习算法

    leetcode中国 leetcode-study 地址: 考虑维度 时间维度:是指执行当前算法所消耗的时间,我们通常用「时间复杂度」来描述。 空间维度:是指执行当前...SortColors 贪心思想 给小孩分配饼干,最多分配多少个 AssignCook

    Leetcode经典01背包-algo:一些记录

    Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆...Colors 计数排序 | | | 88 Merge So

    leetcode2sumc-LeetCode_py:LeetCode_py

    SortColors 167 - TwoSum2 DCP 75 是一个双指针问题,如果当前项为 0,则使用 p1 p2 指向开始和结束,然后与开始交换,如果当前项目为 2,则与结束交换。 167是同一个想法 02/01/2020 16 -3SumClosest 344 - Reverse...

    leetcode卡-Leetcode-solutions:LeetCodeDS日常挑战的解决方案

    leetcode卡Leetcode-解决方案 LeetCode DS 日常挑战的解决方案 问题陈述 1.InvertBinaryTree - 2.子序列- 3.SearchInsertPosition - 4.SortColors - 5.单号——

    purescript-colors:转换,操纵,分析,混合,色阶,配色方案

    $&gt; colors (colorScale HSL hotpink Nil darksalmon) 5 " #ff69b4 " : " #fa6d99 " : " #f47182 " : " #ef7d76 " : " #e9967a " : Nil &gt; toHexString &lt;$&gt; (sortBy (comparing luminance) [black, white, blue, ...

    color-sorter:按色相对CSS颜色排序,然后按饱和度排序

    sort ( colorSort . sortFn )// Or: // sorted = colorSort(colors)// =&gt; sorted:// [// 'red',// 'hsl(0, 10%, 60%)',// '#000'// ]例子这些示例可以在上看到,该程序包用于对颜色进行排序。CSS技巧粉碎杂志 引导...

    VB6_逆向工程.pdf

    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 ...

Global site tag (gtag.js) - Google Analytics