[leetcode sort]75. 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.
解法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的更多相关文章
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- 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 ...
- 【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 ...
- 【leetcode】75. Sort Colors
题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- [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 in-place so that objects of the ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
随机推荐
- HDU 1073 Online Judge (字符串处理)
题目链接 Problem Description Ignatius is building an Online Judge, now he has worked out all the problem ...
- D. Sum in the tree(树形+贪心)
题目链接;http://codeforces.com/contest/1099/problem/D 题目大意:给出一棵树,每个节点到根节点的路径上经过的所有点的权值之和,其深度为偶数的节点的信息全部擦 ...
- UNIX网络编程 第4章 基本TCP套接字编程
本章的几个函数在很大程度上展示了面向对象与面向过程的不同之处.
- Spring编程式和声明式事务实例讲解
Java面试通关手册(Java学习指南):https://github.com/Snailclimb/Java_Guide 历史回顾: 可能是最漂亮的Spring事务管理详解 Spring事务管理 S ...
- dstat 服务器性能查看命令【转】
一. 安装和简解 # yum -y install dstat# dstat CPU状态:CPU的使用率.这项报告更有趣的部分是显示了用户,系统和空闲部分,这更好地分析了CPU当前的使用状况.如果你看 ...
- SQL 存储过程分页
CREATE PROC p_Team_GetTemaList @pageindex INT , @pagesize INT , @keywords VARCHAR(200) , --模糊查询 名称 标 ...
- Team Foundation Server 2010服务器安装
本安装指南使用Windows Server 2008企业版为基础,安装Windows Server 2008 SP2(必须),在此操作系统环境上进行TFS2010的安装与配置. 三.系统用户设置 1. ...
- linux用户权限 -> 系统特殊权限
set_uid 运行一个命令的时候,相当于这个命令的所有者,而不是执行者的身份. suid的授权方法 suid 权限字符s(S),用户位置上的x位上设置. 授权方法: passwd chmod u+s ...
- MySQL 联合查询
联合查询:将多次查询(多条select语句), 在记录上进行拼接(字段不会增加) 基本语法:多条select语句构成: 每一条select语句获取的字段数必须严格一致(但是字段类型无关) 语法 Sel ...
- Scrapy项目之User timeout caused connection failure(异常记录)
Windows 10家庭中文版,Python 3.6.4,Scrapy 1.5.0, 提示:此文存在问题,真正测试, 请勿阅读, 07-14 14:26更新: 经过两个多小时的测试,发现此问题的原因是 ...