题目来源:

  https://leetcode.com/problems/sort-colors/


题意分析:

  给定n个颜色,红色,白色和蓝色。分别用0,1,2代替,将这些颜色排序,0在1前,1在2前。


题目思路:

  记录一下起始位置和末尾。遍历一下输入,如果是2就放到末尾,末尾-1,如果是0,那么放到开始位置,其实位置+1.


代码(Python):

  

 class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i,start,end = 0,0,len(nums) - 1
while i <= end:
if nums[i] == 2:
nums[i],nums[end] = nums[end],nums[i]
end -= 1
elif nums[i] == 0:
nums[i],nums[start] = nums[start],nums[i]
start += 1
i += 1
else:
i += 1

转载请注明出处:http://www.cnblogs.com/chruny/p/5069860.html

[LeetCode]题解(python):075-Sort Colors的更多相关文章

  1. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  2. Java for LeetCode 075 Sort Colors

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

  3. 【LeetCode】075. Sort Colors

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

  4. [leetcode.com]算法题目 - Sort Colors

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

  5. 【LeetCode with Python】 Sort List

    博客域名:http://www.xnerv.wang 原题页面:https://oj.leetcode.com/problems/sort-list/ 题目类型: 难度评价:★ 本文地址:http:/ ...

  6. LeetCode(75) Sort Colors

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

  7. 075 Sort Colors 分类颜色

    给定一个包含红色.白色和蓝色,且含有 n 个元素的数组,对它们进行排序,使得相同颜色的元素相邻,颜色顺序为红色.白色.蓝色.此题中,我们使用整数 0, 1 和 2 分别表示红色,白色和蓝色.注意:不能 ...

  8. LeetCode题解之Insertion Sort List

    1.题目描述 2.题目分析 利用插入排序的算法即可.注意操作指针. 3.代码 ListNode* insertionSortList(ListNode* head) { if (head == NUL ...

  9. 【LeetCode题解】排序

    1. 排序 排序(sort)是一种常见的算法,把数据根据特定的顺序进行排列.经典的排序算法如下: 冒泡排序(bubble sort) 插入排序(insertion sort) 选择排序(selecti ...

  10. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

随机推荐

  1. JY的题目(水)

    JY的题目[问题背景]一天,JY觉得DZY智商太低下,决定和他离婚,除非DZY做出来她出的题目.DZY当然非常想和JY在一起,所以他只好又去请计算机大神WJC帮忙,WJC已经帮过他N多次忙了,不想再帮 ...

  2. UI_拖动View

    方法一 在touchesMoved中 // 获取到触摸的手指 UITouch *touch = [touches anyObject]; // 获取集合中对象 // 获取開始时的触摸点 CGPoint ...

  3. 【和我一起学习Unity3D】Unity3D的坐标控制

    坐标这个东西,在Unity3D里面是分为几个类的,各自是Vector2,Vector3.Vector4:含义各自是:二维坐标系,三维坐标系,四维坐标系.一般做游戏呢,用到的最多的就是Vector3了. ...

  4. mysql关联更新

    update tb_sdd_info a,tb_bnm_evian_info b set a.username=b.username where a.username=b.memberno and  ...

  5. SQL学习之联结表的使用

    1.简介:"联结(join)表"是SQL最强大的功能之一.联结是利用SQL的SELECT能执行的最重要的操作,很好地理解联结及其语法是学习SQL的极为重要的部分! 在能够有效的使用 ...

  6. SQL Server数据库连接字符串整理

    1.sql验证方式的 Data Source=数据源;Initial Catalog= 数据库名;UserId=sql登录账号;Password=密码; Eg: Data Source=.;Initi ...

  7. Git - 常用技能

    参考: http://wuchong.me/blog/2015/03/30/git-useful-skills/

  8. 黑马12期day01之html&css

    html注释:<!-- --> html中不支持空格.回车.制表符都会被解析成一个空格 <pre></pre>标签内以上三个会被正常解析. <font> ...

  9. Russia

    一.莫斯科 Moscow(Москва) 24日-周四(Day1) 1.广州 9:30公园前地铁站内集合,10:30分到达白云机场(CAN机场),12:50起飞. 18:20经停乌鲁木齐(URC机场) ...

  10. 一个失败的操作系统MULTICS

    Unix的诞生和Multics(Multiplexed Information and Computing System)是有一定渊源的.当时开发者Brian Kernighan开玩笑地戏称这个不完善 ...