[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分别表示红色, ...
随机推荐
- redis写定时任务获取root权限
前提: 1.redis由root用户启动. 2.开启cron的时候,/var/spool/cron linux机器下默认的计划任务,linux会定时去执行里面的任务. 启动服务 :/sbin/serv ...
- Anaconda+django写出第一个web app(三)
前面我们已经建立了模型Tutorial,也已经可以用Navicat Premium打开数据看查看数据,接下来我们通过建立admin账户来上传数据. 在命令行执行如下命令来创建用户: python ma ...
- InnoDB 引擎独立表空间
InnoDB 引擎独立表空间 使用过MySQL的同学,刚开始接触最多的莫过于MyISAM表引擎了,这种引擎的数据库会分别创建三个文件:表结构.表索引.表数据空间.我们可以将某个数据库目录直接迁移到 ...
- SpringCloud之Eureka(注册中心集群篇)
一:集群环境搭建 第一步:我们新建两个注册中心工程一个叫eureka_register_service_master,另一个叫eureka_register_service_backup eureka ...
- Spiral Matrix I & II
Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...
- Linux内核触摸屏驱动--多点触摸 【转】
转自:http://blog.chinaunix.net/uid-24227137-id-3127126.html 简介 为了使用功能强大的多点触控设备,就需要一种方案去上报用户层所需的详细的手指 ...
- 一、springboot入门
构建spring boot工程一般采用两种方式 gradle .maven maven方式 pom.xml spring-boot-starter:核心模块,包括自动配置支持.日志和YAML spri ...
- Linux_僵尸进程、挂载、block块、inode号
僵尸进程: 基本概念: 进程分为父进程和子进程 父进程一死 子进程都会死 杀死主进程的时候 子进程也会被杀死 僵尸进程:主进程被杀死了 但是子进程还活着 子进程不会占用cpu但还是会占着内存 ...
- tomcat运行内存溢出问题
tomcat 运行内存配置优化 /opt/apache-tomcat-7.0.65/bin/catalina.sh:JAVA_OPTS="$JAVA_OPTS -Dfile.encoding ...
- 本地为Windows,使用Xshell登录Linux云主机
以某东的云主机为实例 1. 下载并安装远程登录软件 下载Xshell软件 下载后双击xshell5_5.0.1332.exe进行安装 2. 安装完成,打开Xshell,并点击新建,根据要求输入相应参数 ...