Skip to content

Commit 8ae037c

Browse files
committed
Update 25. 颜色分类.md
1 parent fb99aec commit 8ae037c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

25. 颜色分类.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
***给定一个包含红色、白色和蓝色、共 n 个元素的数组 nums ,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。***
22

3+
```
4+
输入:nums = [2,0,2,1,1,0]
5+
输出:[0,0,1,1,2,2]
6+
```
7+
38
```
49
#单指针+对数组进行两次遍历。
510
#在第一次遍历中,我们将数组中所有的 0 交换到数组的头部。
@@ -19,8 +24,7 @@ class Solution:
1924
        for i in range(ptr, n):
2025
            if nums[i] == 1:
2126
                nums[i], nums[ptr] = nums[ptr], nums[i]
22-
                ptr += 1
23-
        return nums                
27+
                ptr += 1             
2428
```
2529

2630
```

0 commit comments

Comments
 (0)