[Leetcode Week2]Sort Colors

时间:2022-10-09 18:53:11

Sort Colors题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/sort-colors/description/


Description

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.

Solution

void sortColors(int* nums, int numsSize) {
int b[3] = {0};
int i;
for (i = 0; i < numsSize; i++)
b[nums[i]]++;
for (i = 0; i < b[0]; i++)
nums[i] = 0;
for (i = b[0]; i < b[0] + b[1]; i++)
nums[i] = 1;
for (i = b[0] + b[1]; i < numsSize; i++)
nums[i] = 2;
}

解题描述

这道题一上手想到的解法就是桶排序。因为数字的范围是确定的且较小。但是跑出来的时间挺长的。尝试手写快排看看能不能提速,发现还是差不多的,所以Solution只给出了写起来容易点的桶排序。

[Leetcode Week2]Sort Colors的更多相关文章

  1. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  2. LeetCode 75&period; Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  3. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  4. LeetCode 75&period; Sort Colors(排序颜色)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. &lbrack;LeetCode题解&rsqb;&colon; Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  6. &lbrack;LeetCode&rsqb; 75&period; Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. Leetcode 75&period; Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. 【leetcode】Sort Colors&lpar;middle&rpar;&star;

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. Java for LeetCode 075 Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. Java List与数组之间的转换

    http://blog.csdn.net/kingzone_2008/article/details/8444678

  2. UVALive6900 Road Repair(树的点分治)

    题目大概说一棵树,树边有费用和收益两个属性,求一条收益和最大的路径满足费用和不超过C. 树上任意两点的路径都可以看成是过某一个子树根的路径,显然树分治. 治的时候要解决的一个问题是,找到费用小于等于某 ...

  3. 【转载】R6034错误,C Runtime Error

    能查到的解决方法都在里面有提及: 我是使用 stdafx.h加入这句 code #pragma comment(linker, "\"/manifestdependency:typ ...

  4. cocos2d(背景图片循环滚动)

    背景图片循环滚动 使用action 实现的: 主要有两个背景图片交替循环滚动:我选的两个背景图片的宽度都是1024的 ,所以定义了#define BGIMG_WIDTH 1024 代码如下: 在Hel ...

  5. websocket学习和使用

    1)WebSocket介绍 HTML5 Web Sockets规范定义了Web Sockets API,支持页面使用Web Socket协议与远程主机进行全双工的通信.它引入了WebSocket接口并 ...

  6. 学生信息管理系统&lpar;c语言&rpar;

    ①注意: 程序中使用了sleep()函数.system()函数 关于 sleep() 函数 sleep() 函数的头文件和用法会因环境的不同而有所不同. 具体见-sleep()函数功能及用法 关于sy ...

  7. ZYThumbnailTableView类似于小型阅读器

    Demo github地址: https://github.com/liuzhiyi1992/ZYThumbnailTableView  原文地址:http://zyden.vicp.cc/zythu ...

  8. Saltstack&lowbar;使用指南04&lowbar;数据系统-Grains

    1. 主机规划 Grains文档 https://docs.saltstack.com/en/latest/topics/grains/index.html 注意事项 修改了master或者minio ...

  9. springboot 传List参数

    最近项目有个需求,前台需要传list参数请求controller接口,一开始直接使用ResponseBody注解,但实践下来发现参数没有传到controller. 现将处理方式记录如下:  1.前台 ...

  10. 20171228 C&num;值类型和引用类型

    public class RefPoint //定义的引用类型 { public int x; public RefPoint(int x) { this.x = x; } } public stru ...