题目来源:

  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. netbeans字体与颜色配置模板相关网站

    NetBeans Themes -Color Schemes of the NetBeans IDE NetBeans ThemeBuilder

  2. Ext.Net 使用总结之GridPanel的删除事件

    1.关于Ext.net中GridPanel的删除事件 首先是GridPanel,如下: <ext:GridPanel ID="GridPanel1" runat=" ...

  3. 尝试解决IIS问题一些方法

    尝试解决IIS问题一些方法 在控制面板中安装相关功能.添加相关角色 Win下注册IIS: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_re ...

  4. GWT RPC机制

    GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...

  5. C++/C#结构体转化-二维数组-bytes To Strings

    C++结构体 typedef struct VidyoClientRequestGetWindowsAndDesktops_ { /*! The number of application windo ...

  6. mysql save or update

    原文:http://www.bitscn.com/pdb/mysql/201503/473268.html 背景   在平常的开发中,经常碰到这种更新数据的场景:先判断某一数据在库表中是否存在,存在则 ...

  7. 推荐一款手机端的图片滑动插件iSlider

    首先先放出中文官方地址   http://be-fe.github.io/iSlider/index.html 这是demo 众所周知,移动端的图片滑动插件有很多,为什么我要推荐这个iSlider呢? ...

  8. Javascript类型检测

    原地址 http://www.cnblogs.com/fool/archive/2010/10/07/javascrpt.html 开门见山,我们先来看一下代码: var is = function ...

  9. JSP三大常用对象request、response、session

    1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求, 然后做出响应.它是HttpServletRequest类的实例. 序号方法说明 objectgetA ...

  10. 使用css3和伪元素制作的一个立体导航条

    使用css3和伪元素制作的一个立体导航条供大家参考,代码如下: <!doctype html> <html lang="en"> <head> ...