Leetcode 75 Sort Colors
2015-06-16 23:12
330 查看
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
为0的前端插入,为2的后端插入,即可完成不稳定的排序。
注意下标的变化。
这里用Ruby的pop()不太好写。
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
为0的前端插入,为2的后端插入,即可完成不稳定的排序。
注意下标的变化。
这里用Ruby的pop()不太好写。
def sortColors(self, A): i = 0 for j in range(len(A)): if A[i] == 0: A.insert(0, A.pop(i)) elif A[i] == 2: A.append(A.pop(i)) i -= 1 i += 1
相关文章推荐
- Theano2.1.10-基础知识之循环
- python中if __name__ == '__main__': 的解析
- 黑马程序员—————JAVA语言j基础Date日期类和Calendar日类类详解。
- Filter Chain in J2EE : revise response header
- LeetCode —— Merge k Sorted Lists
- java 异常处理
- thinking in java 之 public private protect
- 【吾日三省吾身】2015.6.16-涅槃行动第二十九天
- 2015061608 - 思考工作
- 用栈实现表达式求值
- 2015061607 - 前后端接口校验
- Android项目之性别选择
- 哈夫曼编码
- 第一篇,该起什么名字好呢?
- 设置MySQL复制时,replicate-ignore-db模式下如何正常工作
- 【codechef】Steady tables(dp)
- SpringMVC理解之一:应用上下文webApplicationContext
- 菜鸟学Android笔记(三十六):JSP入门
- ReactiveCocoa & MVVM 学习总结一
- IOS Exception2 this class is not key value coding-compliant for the key Click