【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 = 0nums += ['#']while inx < len(nums):if nums[inx] == 0:del nums[inx]nums.insert(0,0)inx += 1elif nums[inx] == 2:del nums[inx]nums.append(2)elif nums[inx] == '#':del nums[inx]breakelse:inx += 1
转载于:https://www.cnblogs.com/seyjs/p/9617370.html
【leetcode】75. Sort Colors相关推荐
- 【leetcode】75.颜色分类(多种解法,超详细图文解析)
75. 颜色分类 难度中等 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,**原地**对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 ...
- 【leetcode】148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...
- [LeetCode]75.Sort Colors
[题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...
- 【排序】LeetCode 75. Sort Colors
LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【LeetCode】【HOT】739. 每日温度(栈)
[LeetCode][HOT]739. 每日温度 文章目录 [LeetCode][HOT]739. 每日温度 package hot;import java.util.ArrayDeque; impo ...
- 【LeetCode】【HOT】49. 字母异位词分组(递归)
[LeetCode][HOT]49. 字母异位词分组 文章目录 [LeetCode][HOT]49. 字母异位词分组 package hot;import java.util.ArrayList; i ...
最新文章
- vim中权限不足时不用退出而强制保存
- CSS before / after伪元素
- Windows Server 2008中部署AD
- 基于FT5x06嵌入式Linux电容触摸屏驱动
- spark sql 1.2.0 测试
- 一文了解 ng-template, ng-content, ng-container, 和 *ngTemplateOutlet的区别
- c++类对象的创建方式
- 3、MySQL的数据类型介绍
- linux下的正则表达式
- 汽车之家港股上市发行价定为176.3港元 募资35.6亿港元
- jQuery中 trigger() 使用心得
- 红黑树的删除_从红黑树的本质出发,彻底理解红黑树!
- Java程序员在写SQL程序时候常犯的10个错误
- chrome最新Android版本,谷歌chrome浏览器安卓版下载,谷歌chrome浏览器安卓最新版本下载地址 v70.0.3538.110 - 浏览器家园...
- mysql preparedstatement 批量update_Statement和PreparedStatement批量更新
- 家用简单电线路图_这6张图在手,简单的家庭电路,电线还怕不会装?不存在的!...
- Pytorch框架学习记录10——线性层
- java中length和length()的区别
- day 54 django-图书管理系统_出版社的管理(展示,新增,删除,编辑)
- 数据软件分析(一)——静态分析