Skip to content

Commit

Permalink
leetcode: Add index file to 0075
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Apr 29, 2024
1 parent 32668b4 commit 64c5732
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- [第二部分: 算法](algs/index.md)
- [分析算法](algs-analysis/index.md)
- [排序 Sorting](sorting/index.md)
- [排序相关的问题列表](leetcode/tags/sorting.md)
- [查找](searching/index.md)
- [二分查找 Binary Search](searching/binary-search/index.md)
- [二分查找相关的问题列表](leetcode/tags/binary-search.md)
Expand Down Expand Up @@ -90,6 +91,7 @@
- [链表](leetcode/tags/linked-list.md)
- [数学](leetcode/tags/math.md)
- [滑动窗口](leetcode/tags/sliding-window.md)
- [排序](leetcode/tags/sorting.md)
- [](leetcode/tags/stack.md)
- [字符串](leetcode/tags/string.md)
- [双指针](leetcode/tags/two-pointers.md)
Expand All @@ -101,6 +103,7 @@
- [0015. 三数之和 3Sum](leetcode/0015.3sum/index.md)
- [0026. 删除有序数组中的重复项 Remove Duplicates from Sorted Array](leetcode/0026.remove-duplicates-from-sorted-array/index.md)
- [0067. 二进制求和 Add Binary](leetcode/0067.add-binary/index.md)
- [0075. 颜色分类 Sort Colors](leetcode/0075.sort-colors/index.md)
- [0080. 删除排序数组中的重复项 II Remove Duplicates from Sorted Array II](leetcode/0080.remove-duplicates-from-sorted-array-ii/index.md)
- [0088. 合并两个有序数组 Merge Sorted Array](leetcode/0088.merge-sorted-array/index.md)
- [0101-0200](leetcode/orders/0101-0200.md)
Expand Down
31 changes: 31 additions & 0 deletions src/leetcode/0075.sort-colors/assets/three-pointers.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<mxfile host="Electron" modified="2024-04-29T03:54:10.876Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/24.2.5 Chrome/120.0.6099.109 Electron/28.1.0 Safari/537.36" etag="pgn4Ngm23POuHrjT8gHF" version="24.2.5" type="device">
<diagram name="Page-1" id="FFz-IKYpVYwWqBeR8RZv">
<mxGraphModel dx="1434" dy="835" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="X5G5Bk8h1uoy_RTIXZDS-1" value="" style="group" vertex="1" connectable="0" parent="1">
<mxGeometry x="280" y="360" width="240" height="40" as="geometry" />
</mxCell>
<mxCell id="JE0PaYjt03FcT6Js5zKv-1" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="X5G5Bk8h1uoy_RTIXZDS-1" vertex="1">
<mxGeometry width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="JE0PaYjt03FcT6Js5zKv-2" value="0" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="X5G5Bk8h1uoy_RTIXZDS-1" vertex="1">
<mxGeometry x="40" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="JE0PaYjt03FcT6Js5zKv-3" value="2" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="X5G5Bk8h1uoy_RTIXZDS-1" vertex="1">
<mxGeometry x="80" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="JE0PaYjt03FcT6Js5zKv-4" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="X5G5Bk8h1uoy_RTIXZDS-1" vertex="1">
<mxGeometry x="120" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="JE0PaYjt03FcT6Js5zKv-5" value="1" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="X5G5Bk8h1uoy_RTIXZDS-1" vertex="1">
<mxGeometry x="160" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="JE0PaYjt03FcT6Js5zKv-6" value="0" style="rounded=0;whiteSpace=wrap;html=1;shadow=0;strokeColor=#6c8ebf;strokeWidth=1;fontFamily=Ubuntu;fontSize=16;fontStyle=0;fillColor=#dae8fc;" parent="X5G5Bk8h1uoy_RTIXZDS-1" vertex="1">
<mxGeometry x="200" width="40" height="40" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
19 changes: 19 additions & 0 deletions src/leetcode/0075.sort-colors/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 0075. 颜色分类 Sort Colors

[问题描述](../problems/0075.sort-colors/content.html)

## 靠拢型双指针

靠拢型双指针是一种常用方法, 这个解法是它的变体, 叫[DNF](../../two-pointers/close-up.md).

```rust
{{#include src/main.rs:7:40 }}
```

## 排序法

各种常见的排序算法都可以, 比如括入排序, 选择排序. 因为这毕竟是一个排序题.

```rust
{{#include src/main.rs:47:56 }}
```
123 changes: 109 additions & 14 deletions src/leetcode/0075.sort-colors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@

#![allow(clippy::ptr_arg)]

pub fn sort_colors(nums: &mut Vec<i32>) {
let mut low = 0;
// 靠拢型双指针
// Dutch National Flag, DNF
// three-way partition
pub fn sort_colors1(nums: &mut Vec<i32>) {
assert!(!nums.is_empty());

// 双指针的变形, 三指针
// left 用于指向数组中为0的元素的右侧
let mut left = 0;
// mid 用于遍历数组
let mut mid = 0;
let mut high = nums.len() - 1;
while mid <= high {
// right 用于指向数组中为2的元素的左侧
let mut right = nums.len() - 1;

// 遍历数组
while mid <= right {
if nums[mid] == 0 {
nums.swap(mid, low);
low += 1;
nums.swap(mid, left);
mid += 1;
// 左边的指针往右移一下
left += 1;
} else if nums[mid] == 2 {
nums.swap(mid, high);
if high > 0 {
high -= 1;
nums.swap(mid, right);
// 右边的指针往左移一下
if right > 0 {
right -= 1;
} else {
break;
}
Expand All @@ -26,20 +39,102 @@ pub fn sort_colors(nums: &mut Vec<i32>) {
}
}

fn check_solution() {
// 调用数组的排序方法, 这个不符合要求.
pub fn sort_colors2(nums: &mut Vec<i32>) {
nums.sort();
}

// 选择排序 Selection Sort
pub fn sort_colors3(nums: &mut Vec<i32>) {
for i in 0..(nums.len() - 1) {
for j in i..nums.len() {
if nums[i] > nums[j] {
nums.swap(i, j);
}
}
}
}

// 插入排序 Insertion Sort
pub fn sort_colors4(nums: &mut Vec<i32>) {
for i in 1..nums.len() {
let mut j = i;
while j > 0 && nums[j - 1] > nums[j] {
nums.swap(j - 1, j);
j -= 1;
}
}
}

// 冒泡排序 Bubble Sort
pub fn sort_colors5(nums: &mut Vec<i32>) {
for i in 0..(nums.len() - 1) {
let mut swapped = false;
for j in 0..(nums.len() - 1 - i) {
if nums[j] > nums[j + 1] {
nums.swap(j, j + 1);
swapped = true;
}
}

if !swapped {
break;
}
}
}

pub type SolutionFn = fn(&mut Vec<i32>);

fn check_solution(func: SolutionFn) {
let mut nums = vec![2, 0, 2, 1, 1, 0];
sort_colors(&mut nums);
func(&mut nums);
assert_eq!(&nums, &[0, 0, 1, 1, 2, 2]);

let mut nums = vec![2, 2];
sort_colors(&mut nums);
func(&mut nums);
assert_eq!(&nums, &[2, 2]);

let mut nums = vec![2];
sort_colors(&mut nums);
func(&mut nums);
assert_eq!(&nums, &[2]);
}

fn main() {
check_solution();
check_solution(sort_colors1);
check_solution(sort_colors2);
check_solution(sort_colors3);
check_solution(sort_colors4);
check_solution(sort_colors5);
}

#[cfg(test)]
mod tests {
use super::{
check_solution, sort_colors1, sort_colors2, sort_colors3, sort_colors4, sort_colors5,
};

#[test]
fn test_sort_colors1() {
check_solution(sort_colors1);
}

#[test]
fn test_sort_colors2() {
check_solution(sort_colors2);
}

#[test]
fn test_sort_colors3() {
check_solution(sort_colors3);
}

#[test]
fn test_sort_colors4() {
check_solution(sort_colors4);
}

#[test]
fn test_sort_colors5() {
check_solution(sort_colors5);
}
}
9 changes: 9 additions & 0 deletions src/leetcode/tags/sorting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 排序

## 简单

## 中等

1. [0075. 颜色分类 Sort Colors](../0075.sort-colors/index.md)

## 困难
18 changes: 9 additions & 9 deletions src/leetcode/tags/two-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ TODO:

1. [0011. 盛最多水的容器 Container With Most Water](../0011.container-with-most-water/index.md)
2. [0015. 三数之和 3Sum](../0015.3sum/index.md)
3. [0080. 删除排序数组中的重复项 II Remove Duplicates from Sorted Array II](../0080.remove-duplicates-from-sorted-array-ii/index.md)
4. [0167. 两数之和 II - 输入有序数组 Two Sum II - Input Array Is Sorted](../0167.two-sum-ii-input-array-is-sorted/index.md)
5. [0532.数组中的数对 K-diff Pairs in an Array](../0532.k-diff-pairs-in-an-array/index.md)
3. [0075. 颜色分类 Sort Colors](../0075.sort-colors/index.md)
4. [0080. 删除排序数组中的重复项 II Remove Duplicates from Sorted Array II](../0080.remove-duplicates-from-sorted-array-ii/index.md)
5. [0167. 两数之和 II - 输入有序数组 Two Sum II - Input Array Is Sorted](../0167.two-sum-ii-input-array-is-sorted/index.md)
6. [0532.数组中的数对 K-diff Pairs in an Array](../0532.k-diff-pairs-in-an-array/index.md)

TODO:

1. [31. 下一个排列](https://leetcode.com/problems/next-permutation)
2. [75. 颜色分类](https://leetcode.com/problems/sort-colors)
3. [142. 环形链表 II](https://leetcode.com/problems/linked-list-cycle-ii)
4. [443. 压缩字符串](https://leetcode.com/problems/string-compression)
5. [524. 通过删除字母匹配到字典里最长单词](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting)
6. [986. 区间列表的交集](https://leetcode.com/problems/interval-list-intersections)
7. [1498. 满足条件的子序列数目](https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition)
2. [142. 环形链表 II](https://leetcode.com/problems/linked-list-cycle-ii)
3. [443. 压缩字符串](https://leetcode.com/problems/string-compression)
4. [524. 通过删除字母匹配到字典里最长单词](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting)
5. [986. 区间列表的交集](https://leetcode.com/problems/interval-list-intersections)
6. [1498. 满足条件的子序列数目](https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition)
5 changes: 5 additions & 0 deletions src/two-pointers/assets/Flag_of_the_Netherlands.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/two-pointers/assets/sort-colors.rs
9 changes: 9 additions & 0 deletions src/two-pointers/close-up.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
- 根据题目要求, 选中左右两个指针中的一个, 往中间靠靠拢 (`left += 1` 或者 `right -= 1`, 另一个指针不动
- 直到循环中止

## Dutch National Flag, DNF

这是上面方法的一个变形, 可以查看问题 [0075. 颜色分类 Sort Colors](../leetcode/0075.sort-colors/index.md),
这个方法用于实现三路分区 (three-way partition).

![The Dutch national flag](assets/Flag_of_the_Netherlands.svg)

访问 [wikipedia](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) 查看对应的介绍.

## 相关问题

- [0011. 盛最多水的容器 Container With Most Water](../../leetcode/0011.container-with-most-water/index.md)

0 comments on commit 64c5732

Please sign in to comment.