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 ...
随机推荐
- Linux FTP的安装与权限配置
ftp安装部分,操作步骤如下: 1.切换到root用户 2.查看是否安装vsftp,我这个是已经安装的. [root@localhost vsftpd]# rpm -qa |grep vsftpd v ...
- Git命令的总结
Git 是当前最流行的版本控制程序之一,文本包含了 Git 的一些基本用法 创建 git 仓库 初始化 git 仓库 mkdir project # 创建项目目录cd project # 进入到项 ...
- Windows向Linux上传文件夹
1.将文件夹压缩成.tar.gz文件: 安装7-Zip,选择要压缩的文件夹--右键--“7-Zip”--“添加到压缩包...”,压缩格式选择“tar”, 在此目下就生成了“文件夹名.tar”文件, ...
- 用ajax写机器人聊天的案例
HTML 中的文档 <body> <h3>简单的Ajax实例</h3> <div class="chatbox"> <!-- ...
- Vmware 安装 ghost 版 win 7
很早就弄过vmware,很可惜一直没有仔细研究过,这次要安装一个win7系统,重新又学一下了一下,下面说一下安装的操作步骤吧. 第一步,下载vmware,原版的下载地址就不说了,上传到百度网盘自己下载 ...
- webpack 搭建React(手动搭建)
前言 最近真的都是在瞎学,看到自己不是很明白的东西,都喜欢自己手动去敲1到3遍(晚上下班的时候咯), 瞧,React 基于webpack 搭建,react 官方有一套手脚架工具,我自己也搭建过确实挺 ...
- linux常用基本命令不全
pwd 显示当前目录 ls -lh 显示文件列表,h表示会显示文件的大小 mkdir zhu 创建文件夹zhu rmdir zhu 移除文件夹zhu如果abc中含有其他文件,则不能删除 rm -r ...
- ESP8266WiFiGeneric---通用库--事件和配置
ESP8266WiFiSTAClass .ESP8266WiFiScanClass .ESP8266WiFiAPClass 可以访问 ESP8266WiFiGenericClass的private和p ...
- raster导入postgres Windows命令
cmd命令行 raster2pgsql -s 4326 -I -C -M C:\Users\tt\Downloads\tmean_19_tif\*.tif -F -t 256x256 tmean_19 ...
- 如何修改 tomcat 端口号?
一.tomcat默认端口 tomcat默认的端口是8080,还会占用8005,8009和8443端口.如果已经启动了tomcat,再启动另一个tomcat就会发现 这些端口已经被占用了,这个时候就需要 ...