【leetcode】75. Sort Colors
题目如下:

解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部。
代码如下:
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
inx = 0
nums += ['#']
while inx < len(nums):
if nums[inx] == 0:
del nums[inx]
nums.insert(0,0)
inx += 1
elif nums[inx] == 2:
del nums[inx]
nums.append(2)
elif nums[inx] == '#':
del nums[inx]
break
else:
inx += 1
【leetcode】75. Sort Colors的更多相关文章
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】075. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 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】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
随机推荐
- oracle基础sql
二.SQL Structur query language 结构化查询语言,是操作关系型数据库中的对象. DDL(Data definition language 数据定义语言),用于建表或删表操作, ...
- day29—JavaScript中DOM的基础知识应用
转行学开发,代码100天——2018-04-14 JavaScript中DOM操作基础知识即对DOM元素进行增删改操作.主要表现与HTML元素的操作,以及对CSS样式的操作.其主要应用知识如下图: 通 ...
- Python模块学习之xlrd、xlutils、openpyxl 读写/追加Excel文件
Python操作Excel的四个工具包 xlrd: 对Excel进行读相关操作,注意只能操作 .xls xlwt: 对Excel进行写相关操作,注意只能操作 .xls,且只能创建一个全新的Excel然 ...
- 爬虫之requests 请求
1.发送不同的请求 import requests r = requests.get('https://www.baidu.com/') r = requests.post('http://httpb ...
- c语言1博客作业12-学期总结
一.我学到的内容 二.收获总结 2.1我的收获 链接: c语言1博客作业01:https://www.cnblogs.com/dy-985211/p/11578914.html c语言1博客作业02: ...
- android handler 调用原理
1,调度原理 andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageExchange) ...
- Linux命令行基础操作
目录 1.打开终端命令行 2.常用快捷键 2.1 tab键 2.2 Ctrl+c组合键 2.3 Ctrl+d组合键 2.4Ctrl+Shift+c组合键和Ctrl+Shift+v组合键 2.5图形界面 ...
- an安装jenkins时遇到ERROR: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin的问题
# sudo /etc/init.d/jenkins restartERROR: No Java executable found in current PATH: /bin:/usr/bin:/sb ...
- sed查找实例:mysql_process.sh
标准 #!/bin/bash # FILE_NAME=/home/roo/Desktop/shell_code/day6/my.cnf # 获取所有的片段 function get_all_segme ...
- HNCPC2019部分题解
ProblemSet 签到题就不写了. C. Distinct Substrings 先对原串建出SAM,map存边. 由于这题相当于添加一个字符再删除这个字符,添加下一个字符,所以每次都暴力跳后缀链 ...