Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8a3649c

Browse files
committed
commit solution 75
1 parent d6a324c commit 8a3649c

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

‎index-tags.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
1414
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
1515
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
16+
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
1617
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1718
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
1819
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
@@ -81,6 +82,7 @@
8182
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
8283
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
8384
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
85+
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
8486
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
8587
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
8688
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
@@ -106,6 +108,9 @@
106108

107109
#### **排序**
108110

111+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
112+
| --- | --- | --- | --- | --- |
113+
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
109114
#### **队列**
110115

111116
#### **位运算**

‎index-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@
444444
| [71](https://leetcode-cn.com/problems/simplify-path) | [简化路径](/solution/1-99/0071.simplify-path/) | ``,`字符串` | <font color=blue>中等</font> |
445445
| [73](https://leetcode-cn.com/problems/set-matrix-zeroes) | [矩阵置零](/solution/1-99/0073.set-matrix-zeroes/) | `数组` | <font color=blue>中等</font> |
446446
| [74](https://leetcode-cn.com/problems/search-a-2d-matrix) | [搜索二维矩阵](/solution/1-99/0074.search-a-2d-matrix/) | `数组`,`二分查找` | <font color=blue>中等</font> |
447-
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> |
447+
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
448448
| [77](https://leetcode-cn.com/problems/combinations) | [组合](/solution/1-99/0077.combinations/) | `回溯算法` | <font color=blue>中等</font> |
449449
| [78](https://leetcode-cn.com/problems/subsets) | [子集](/solution/1-99/0078.subsets/) | `位运算`,`数组`,`回溯算法` | <font color=blue>中等</font> |
450450
| [79](https://leetcode-cn.com/problems/word-search) | [单词搜索](/solution/1-99/0079.word-search/) | `数组`,`回溯算法` | <font color=blue>中等</font> |

‎solution/1-99/0075.sort-colors/README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [75. 颜色分类](https://leetcode-cn.com/problems/sort-colors)
22

33
### 题目描述
4-
<!-- 这里写题目描述 -->
4+
55
<p>给定一个包含红色、白色和蓝色,一共&nbsp;<em>n </em>个元素的数组,<strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地</a></strong>对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。</p>
66

77
<p>此题中,我们使用整数 0、&nbsp;1 和 2 分别表示红色、白色和蓝色。</p>
@@ -25,15 +25,27 @@
2525

2626
### 解题思路
2727

28+
1. 双指针
2829

2930
### 具体解法
3031

31-
<!-- tabs:start -->
3232

3333
#### **Golang**
3434
```go
35-
35+
func sortColors(nums []int) {
36+
l := 0
37+
r := len(nums) - 1
38+
for i := 0; i <= r; i++ {
39+
if nums[i] == 0 {
40+
nums[i], nums[l] = nums[l], nums[i]
41+
l++
42+
}
43+
if nums[i] == 2 {
44+
nums[i], nums[r] = nums[r], nums[i]
45+
i--
46+
r--
47+
}
48+
}
49+
}
3650
```
3751

38-
<!-- tabs:end -->
39-
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=75 lang=golang
5+
*
6+
* [75] 颜色分类
7+
*/
8+
9+
// @lc code=start
10+
func sortColors(nums []int) {
11+
l := 0
12+
r := len(nums) - 1
13+
for i := 0; i <= r; i++ {
14+
if nums[i] == 0 {
15+
nums[i], nums[l] = nums[l], nums[i]
16+
l++
17+
}
18+
if nums[i] == 2 {
19+
nums[i], nums[r] = nums[r], nums[i]
20+
i--
21+
r--
22+
}
23+
}
24+
}
25+
26+
// @lc code=end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSortColors(t *testing.T) {
8+
var ret []int
9+
var nums []int
10+
ret = []int{0, 0, 0, 1, 1, 1, 2, 2, 2}
11+
nums = []int{2, 0, 2, 1, 1, 0, 1, 0, 2}
12+
sortColors(nums)
13+
for i, v := range nums {
14+
if ret[i] != v {
15+
t.Fatalf("case fails %v\n", ret)
16+
}
17+
}
18+
}

‎solution/1-99/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
- [72. 编辑距离](solution/1-99/0072.edit-distance/)
8181
- [73. 矩阵置零](solution/1-99/0073.set-matrix-zeroes/)
8282
- [74. 搜索二维矩阵](solution/1-99/0074.search-a-2d-matrix/)
83-
- [75. 颜色分类](solution/1-99/0075.sort-colors/)
83+
- [75. 颜色分类](solution/1-99/0075.sort-colors/)
8484
- [76. 最小覆盖子串](solution/1-99/0076.minimum-window-substring/)
8585
- [77. 组合](solution/1-99/0077.combinations/)
8686
- [78. 子集](solution/1-99/0078.subsets/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /