`

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

    C语言-leetcode题解之75-sort-colors.c

    而本文要探讨的是,LeetCode中一道特定的算法题目——75题“Sort Colors”的C语言解法。这道题目要求使用C语言编写一个函数,实现对给定数组中的0、1、2三种颜色进行排序。 首先,我们需要了解题目背景。数组中的...

    js-leetcode题解之75-sort-colors.js

    在探讨LeetCode上的75题《Sort Colors》的JavaScript题解之前,需要先了解这个问题的基本背景。LeetCode是一个提供算法和数据结构练习题目的平台,其中75题是一道典型的数组操作题目。其要求是将数组中所有的0、1、2...

    java经典算法系列

    这段代码中,`sortColors`函数使用了双指针技巧,`left`指向已排序区域的末尾,`right`指向未排序区域的开头,`mid`遍历整个数组。遇到0时,交换`left`和`mid`指向的元素;遇到2时,将2与`right`指向的元素交换,...

    python-leetcode题解之075-Sort-Colors

    python python_leetcode题解之075_Sort_Colors

    常见算法介绍、算法刷题解析及代码 简介 本文档详细介绍了常见算法,包括排序算法、搜索算法、动态规划、回溯算法等 每个算法部分提

    一、排序算法 1.1 快速排序 快速排序是一种高效的排序算法,其平均时间... Sort Colors 题目要求将包含0、1、2的数组排序,使用快速排序算法来解决此问题。 面试题 2:最长递增子序列 问题:给定一个未排序的整数

    python-leetcode面试题解之第75题颜色分类-题解.zip

    def sortColors(nums): left, right = 0, len(nums) - 1 while left if nums[left] == 0: left += 1 elif nums[left] == 2: nums[left], nums[right] = nums[right], nums[left] right -= 1 else: left +=...

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

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

    java-leetcode题解之第75题颜色分类.zip

    public void sortColors(int[] nums) { int left = 0, right = nums.length - 1; while (left ) { while (left [left] == 0) { // 遇到红色球,向右移动 left++; } while (left [right] == 2) { // 遇到蓝色...

    Leetcode部分试题解析

    6. **Sort Colors**:将一个包含0、1和2的数组原地排序。可以采用双指针策略,让较小的元素向左移动。 7. **Sort List**:对链表进行排序。可以先将链表转换为数组,然后使用快速排序或归并排序,最后将排序后的...

    c语言-leetcode题解之0075-sort-colors.zip

    “c语言-leetcode题解之0075-sort-colors.zip”是一个压缩包文件,包含了使用C语言编写的所有代码文件,旨在解决LeetCode上的第75题。文件中的代码展示了如何高效地实现“荷兰国旗问题”的解决方案。通过将数组元素...

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

    Microsoft - 微软面试题库.pdf

    - “Sort Colors(颜色分类)”是一个典型的数组排序题目。 - “Largest BST Subtree(最大的二叉搜索树子树)”和“Best Time to Buy and Sell Stock(买卖股票的最佳时机)”考查动态规划。 - “Merge k Sorted...

    Leetcode代码以及解答(2)

    Sort Colors **知识点:** - **问题描述:** - 给定一个只包含 `0`, `1`, `2` 的数组,将其按升序排序。 - **解决方案分析:** - **三指针:** - 定义三个指针 `low`, `mid`, `high`。 - `low` 和 `mid` ...

    _leetcode-python.pdf

    - Sort Colors: 给定一个包含红色、白色和蓝色,一共n个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。 - Combinations: 从n个不同元素中取出k个元素的组合。 - ...

    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): 基础知识:链表如何...

Global site tag (gtag.js) - Google Analytics