[Leetcode Week2]Sort Colors
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的更多相关文章
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【leetcode】Sort Colors(middle)☆
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 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 ...
随机推荐
- 九度OJ--1165(C++)
#include <iostream>#include <string>#include <vector> using namespace std; int mai ...
- 学习人工智能的第五个月[字典学习[Dictionary Learning,DL]]
摘要: 大白话解释字典学习,分享第五个月的学习过程,人生感悟,最后是自问自答. 目录: 1.字典学习(Dictionary Learning,DL) 2.学习过程 3.自问自答 内容: 1.字典学习( ...
- Drools 7.4.1.Final参考手册(十四)集成Spring
集成Spring Drools 6.0重要变更 Drools Spring集成经历了与Drools 6.0的变化完全一致的改造. 以下是一些主要的变化: T*推荐的Drools Spring的前缀已经 ...
- 详细讲解Java中方法的重载和重写
首先讲讲方法的重载: Java的重载就是在类中可以创建多个方法,它们具有相同的名字,但是却有不同的参数. 判断是否重载只有两个条件: 1)相同的方法名 2)不同的参数 具体为: A.方法参数类型不同 ...
- java的命名空间
这个package me.gacl.websocket相当于.net中的namespace命名空间. import 相当于.net中的using,引用命名空间:
- lintcode-93-平衡二叉树
93-平衡二叉树 给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1. 您在真实的面试中是否遇到过这个题? Yes 样例 ...
- Java中Model1和Model2
Model1和Model2是java web的两种架构模式.这两种模式各有优缺点,都有各自适合使用的场景. Model1 首先,从分层的角度说,Model1模式可以看作是由两层组成:视图层和模型层. ...
- mysql yearweek修改开始日期
MySQL 的yearweek函数默认是从周日~周六,需求需要从周一到周日,看了MySQL的文档后,按照如下使用即可更改开始日期. http://dev.mysql.com/doc/refman/5. ...
- 【题解】HAOI2008木棍分割
对于这道题目的两问,第一问直接二分答案求出最短长度.关键在于第二问应当如何求:建立dp方程,dp[i][j]代表到第i个分界线,切了j次(强制在第i处切一刀.这样就不会对后面的状态产生影响).状态转移 ...
- poj3375 Network Connection
Description There are \(M\) network interfaces in the wall of aisle of library. And \(N\) computers ...