leetcode-mid-sorting and searching-75. Sort Colors-NO
mycode 97.95%
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
from collections import Counter
res = Counter(nums)
nums[:res[0]] = [0]*res[0]
nums[res[0]:res[0]+res[1]] = [1]*res[1]
nums[res[0]+res[1]:] = [2]*(res[2])
参考:
思路:i记录0的个数,j记录0和1的个数,for循环是,都先把当前位置赋值为2,当前值其实小于2,就根据i、j把该值放到合适的位置
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
i = j = 0
for k in range(len(nums)):
temp = nums[k]
nums[k] = 2
if temp < 2:
nums[j] = 1
j += 1
if temp == 0:
nums[i] = 0
i += 1
leetcode-mid-sorting and searching-75. Sort Colors-NO的更多相关文章
- 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分别表示红色, ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 【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 颜色排序
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 so that objects of the same colo ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 11.jQuery之淡入淡出效果
知识点:fadeIn fadeOut fadeToggle fadeTo <style> div { width: 150px; height: 300px; background ...
- echarts 给legend图例加个标题式文字设置为普通文本不可点击
legend: [ { orient: "horizontal", // 'vertical' x: "68%", // 'center' | 'left' | ...
- 十一、LaTex中的矩阵
- linux下重启tomcat命令
在Linux系统下,重启Tomcat使用命令操作的! 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看 ...
- EM算法理解的九层境界
EM算法理解的九层境界 EM 就是 E + M EM 是一种局部下限构造 K-Means是一种Hard EM算法 从EM 到 广义EM 广义EM的一个特例是VBEM 广义EM的另一个特例是WS算法 广 ...
- tf.split( )和tf.unstack( )
import tensorflow as tf A = [[1, 2, 3], [4, 5, 6]] a0 = tf.split(A, num_or_size_splits=3, axis=1)#不改 ...
- 【leetcode】1105. Filling Bookcase Shelves
题目如下: We have a sequence of books: the i-th book has thickness books[i][0] and height books[i][1]. W ...
- 【shell】sed处理多行合并
有这么一个题 文件格式 table=t1 name owner address table=t2 id text col1 comment col5 table=t3 prod_name price ...
- PROP_ENTRY_TYPE用法
最近几天刚换了vs2019,然后各种水土不服 这不 刚建了一个ATL组件,发现添加了属性编译不过,提示: 错误 C4995 “PROP_ENTRY”: 名称被标记为 #pragma deprecate ...
- jquery contains选择器 语法
jquery contains选择器 语法 作用::contains 选择器选取包含指定字符串的元素.该字符串可以是直接包含在元素中的文本,或者被包含于子元素中.经常与其他元素/选择器一起使用,来选择 ...