题解 | #颜色分类#
颜色分类
http://www.nowcoder.com/practice/52e04ddb7b5640a8869c2d3da2ad3344
双指针
#coding:utf-8
class Solution:
def sortColor(self , colors ):
# write code here
ptr_red = 0
for i in range(len(colors)):
if colors[i] == 0:
colors[ptr_red], colors[i] = colors[i], colors[ptr_red]
ptr_red += 1
ptr_while = ptr_red
for i in range(ptr_red, len(colors)):
if colors[i] == 1:
colors[ptr_while], colors[i] = colors[i], colors[ptr_while]
ptr_while += 1
return colors

