题目来源:

  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. Android自定义Activity酷炫的动画跳转效果

    两个Activity跳转的时候,自定义翻页效果: Intent intent = new Intent(FirstActivity.this, SecondActivity.class);   sta ...

  2. pwd显示链接文件的真实路径

    1.pwd用于显示当前工作路径 2.pwd -P 用于显示当前的实际工作路径(主要用于链接文件) 加参数难以了理解,看个例子就明白了: 进入链接文件,pwd显示的是链接文件所在的路径,而你不是链接文件 ...

  3. golang之interface(接口)与 reflect 机制

    一.概述 什么是interface,简单的说,interface是一组method的组合,通过interface来定义对象的一组行为: interface类型定义了一组方法,如果某个对象实现了某个接口 ...

  4. WCF:System.Security.Cryptography.CryptographicException: 密钥集不存在

    WCF使用IIS部署时,使用x509证书验证,在创建证书并正确配置程序后,访问出现错误提示: System.Security.Cryptography.CryptographicException: ...

  5. 自学JQuery Mobile的几个例子

    JQuery Mobile是一个用于构建移动Web应用程序的框架,适用于主流的移动设备(智能手机.平板电脑),该框架利用了HTML5和CSS3技术减少了额外的脚本文件的编写.具体JQuery Mobi ...

  6. English - refer to...和refer to...as

    refer to...和refer to...as...本来就是refer的两个固定搭配,这个只能讲讲后两者用法,剩下的就是单独的refer的用法了. 1. refer to sb/sth 指的是/提 ...

  7. office2010安装出错,windows installer服务不能更新一个或多个受保护的windows文件

    转自:http://www.08lr.cn/article/1985.html office2010安装过程中出现如下图错误:windows installer 服务不能更新一个或多个受保护的wind ...

  8. 关于多线程中GCD的使用

    GCD 分为异步和同步 异步: ```objc  dispatch_async (  参数1  , {      } 同步: dispatch_sync( 参数1   , {   } ``` ###参 ...

  9. 对于System.Net.Http的学习(一)——System.Net.Http 简介(转)

    最新在学习System.Net.Http的知识,看到有篇文章写的十分详细,就想转过来,自己记录下.原地址是http://www.cnblogs.com/chillsrc/p/3439215.html? ...

  10. 《Effective C++》Item2:尽量以const,enum,inline替换#define

    1. 宏定义 #define ASPECT_RATIO 1.653 该宏定义ASPECT_RATIO也许从来没有被编译器看到,也许在编译器开始处理源码之前就已经被预处理器替换了.所以记号名称ASPEC ...