class Solution {
public:
void sortColors(vector<int>& nums) {
sort(nums.begin(), nums.end());
}
};

上面这个太弱了,给一个强不了多少的

 class Solution:
def sortColors(self, nums: 'List[int]') -> 'None':
red = 0
white = 0
blue = 0
for n in nums:
if n == 0:
red+=1
elif n == 1:
white += 1
elif n == 2:
blue += 1
else:
print('error')
nums.clear()
nums.extend([0]*red + [1]*white + [2]*blue)

下面给出一个比较强的

 class Solution:
def swap(self,nums,i,j):
nums[i],nums[j] = nums[j],nums[i]
'''
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
''' def sortColors(self, nums: 'List[int]') -> 'None':
n = len(nums)
left = 0
right = n - 1
i = 0
while i <= right:
if nums[i] == 0:
if i != left:
self.swap(nums,i,left)
i += 1
left += 1
elif nums[i] == 1:
i += 1
else:
self.swap(nums,i,right)

注意swap的写法,如果用temp来存,速度会慢很多。

leetcode75的更多相关文章

  1. 【leetcode75】Intersection of Two Arrays(数组的交集)

    题目描述: 给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次 例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 原文描述: ...

  2. [Swift]LeetCode75. 颜色分类 | Sort Colors

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  3. 【leetcode-75】 颜色分类

    (1过,解法不好,看參考荷兰国旗问题解法) 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0 ...

  4. Leetcode75. Sort Colors颜色分类

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  5. LeetCode75 Sort Colors

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  6. leetcode75之颜色分类

    题目描述: 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白 ...

  7. leetcode75:search-a-2d-matrix

    题目描述 请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征: 每一行的数字都从左到右排序 每一行的第一个数字都比上一行最后一个数字大 例如: 对于下面的矩阵: [ [1, 3, ...

  8. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. Problem E: 类的初体验(V)

    Description 定义一个类Data,只有一个int类型的属性和如下方法: 1.   缺省构造函数,将属性初始化为0,并输出"Data's default constructor.&q ...

  2. design

    type Config struct { Item lock } func (*Config) getItem(){ } func (*Config) SetItem(){ } channel : - ...

  3. FCC JS基础算法题(1):Factorialize a Number(计算一个整数的阶乘)

    题目描述: 如果用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积.阶乘通常简写成 n!例如: 5! = 1 * 2 * 3 * 4 * 5 = 120. 算法: function fac ...

  4. 集群容器管理之swarm ---服务管理

    服务管理 # 创建服务docker service create --replicas 1 --name hello busybox # docker service update --args &q ...

  5. JavaScript 基础篇1

    JavaScript引用问题 1:<script>标签引用嵌入html页面中,在外部引用中是JavaScript文件时必须用src属性设置相应的文件的URL.2:在不使用defer和asy ...

  6. c# 号码记录,车友

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. hello1源码解析

    1.选择hello1文件夹并单击“打开项目” 2.展开网页节点,双击index.xhtml文件在编辑器中查看它 index.xhtml文件是facelets应用程序的默认登录页,在典型的facelet ...

  8. python:基本统计值计算(平均数,方差,中位数)

    #CalStatisticsV1.py def getNum(): #获取用户不定长度的输入 nums=[] test=input("请输入要存储的数据(回车退出):") whil ...

  9. vc6.0使用

    1.文件结构 工作空间dsw 工程1    Source file        .cpp,main    Header file        .h    Resource files 工程2   ...

  10. 添加一个pv到vg后,误删新加入的pv,报 pv * not found or rejected by a filter

    问题如下 将某一pv加入vg vgextend cl /dev/xvdb1 然后进入fdisk将xvdb1分区删掉,重新创建pv 使用lvdisplay报警告 [root@localhost ~]# ...