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.

解法1:

统计每个颜色出现的次数,这个方法比较简单易懂

 class Solution(object):
def sortColors(self, nums):
flag = [0,0,0]
for i in nums:
flag[i] += 1
for n in range(flag[0]):
nums[n] = 0
for n in range(flag[1]):
nums[flag[0]+n]=1
for n in range(flag[2]):
nums[flag[0]+flag[1]+n] = 2

解法2:

在评论区总是能发现一些很漂亮的方法,方法和快排比较相似

 class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i,start,end = 0,0,len(nums)-1
while i<=end:
if nums[i]==0:
nums[i]=nums[start]
nums[start]=0
start += 1
elif nums[i]==2:
nums[i]=nums[end]
nums[end]=2
end -= 1
i -= 1 #交换后此位置的数未考虑,要重新考虑
i += 1

解法3:

最帅的是这种方法,类似于快排,指针i,j分别处理以类数据

 class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i,j = 0,0
for k in range(len(nums)):
v = nums[k]
nums[k] = 2
if v<2:
nums[j]=1
j += 1
if v == 0:
nums[i]=0
i += 1

[leetcode sort]75. Sort Colors的更多相关文章

  1. 【一天一道LeetCode】#75. Sort Colors

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 【LeetCode】75. Sort Colors 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...

  3. LeetCode OJ 75. Sort Colors

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

  4. 【LeetCode】75. Sort Colors (3 solutions)

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

  5. 【leetcode】75. Sort Colors

    题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...

  6. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  7. [LeetCode] 75. Sort Colors 颜色排序

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

  8. LeetCode 75. Sort Colors (颜色分类):三路快排

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

  9. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

随机推荐

  1. Understanding the Space Used by ZFS -- (转)

    Understanding the Space Used by ZFS By Brian Leonard on Sep 28, 2010 Until recently, I've been confu ...

  2. C#事件实现文件下载时进度提醒

    C#中的事件是建立在委托的基础上,标准的事件模型应该包括以下几点: 声明一个用于定义事件的委托,这里用系统自带的泛型委托原型EventHandler<TEventArgs>,如:publi ...

  3. jQuery-对标签元素 文本操作-属性操作-文档的操作

    一.对标签元素文本操作 1.1 对标签中内容的操作 // js var div1 = document.getElementById("div1"); div1.innerText ...

  4. 五、springboot单元测试

    1.为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 2. 可以自动测试,可以在项目打包前进行测试校验 3. 可以及时发现因为修改代码导致新的问题的出现,并及时解决 ...

  5. TCP协议端口状态说明:CLOSE-WAIT、TIME-WAIT 、LISTENING、SYN_SENT、ESTABLISHED、LAST-ACK ...

    了解TCP协议端口的连接状态,对排除和定位网络或系统故障会有很大帮助,因此了解一下是有必要的: 一.LISTENING  提供某种服务,侦听远方TCP端口的连接请求,当提供的服务没有被连接时,处于LI ...

  6. 在虚拟机(vmware)上安装CentOS

    第一步是安装虚拟机,这个比较简单就不讲了. 第二步准备CentOS的镜像文件准备安装 第三步安装CentOS: 新建虚拟机 选择自定义配置 选择硬件兼容标准 选择是否让vmware安装操作系统 选择将 ...

  7. fsevents npm install是报错

    npm install 安装插件的时候,fsevents报错,这是node 8.x版本的问题,解决办法,把node 版本切换到6.x

  8. Java多线程-Java多线程概述

    第一章 Java多线程概述 线程的启动 线程的暂停 线程的优先级 线程安全相关问题 1.1 进程与线程 进程:可以将运行在内存中的程序(如exe文件)理解为进程,进程是受操作系统管理的基本的运行单元. ...

  9. Shell学习笔记:<<EOF子命令

    在shell编程中,“EOF”通常与“<<”结合使用,“<<EOF”表示后续的输入作为子命令或子shell的输入,直到遇到“EOF”,再次返回到主调用shell,可将其理解为分 ...

  10. Jmeter的接口测试简介

    一.安装Jmeter                                                          Jmeter官方首页:http://jmeter.apache. ...