Skip to content

Commit

Permalink
perf(max_substring): 对max_substring名字优化 (#40)
Browse files Browse the repository at this point in the history
* perf(max_substring): 对max_substring名字优化

* doc(README.org): 添加README.org

* fmt

* fmt

* fmt

* fmt

* fmt

* fmt

* fmt
  • Loading branch information
donjuanplatinum authored Jul 28, 2024
1 parent 590fbae commit 38d19b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 61 deletions.
45 changes: 0 additions & 45 deletions README.md

This file was deleted.

28 changes: 28 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[[https://crates.io/crates/algori][https://img.shields.io/crates/d/algori.svg]]
[[https://github.com/BarrenSea/algori/fork][https://img.shields.io/github/forks/barrensea/algori.svg]]
[[https://github.com/BarrenSea/algori][https://img.shields.io/github/repo-size/barrensea/algori.svg]]
[[https://github.com/BarrenSea/algori][https://img.shields.io/github/stars/barrensea/algori.svg]]
[[https://github.com/BarrenSea/algori][https://img.shields.io/github/commit-activity/t/barrensea/algori.svg]]
[[https://conventionalcommits.org][https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white.svg]]

** Algorithms
*** Sorting 排序
- [[./doc/sorting/_index.md][InsertionSort 插入排序]]
- [[./doc/sorting/_index.md][SelectionSort 选择排序]]
- [[./doc/sorting/_index.md][BubbleSort 冒泡排序]]
- [[./doc/sorting/_index.md][BinarySort 二分排序]]
- [[./doc/sorting/_index.md][MergeSort 归并排序]]
*** Structures 数据结构
- [[./doc/structure/_index.md][Heap 堆/优先队列]]
- [[./doc/structure/_index.md][LinedList 双向链表]]
- [[./doc/structure/_index.md][二进制链表相加]]
- [[./doc/structure/_index.md][二进制链表相加]]
*** Searching 搜索
- [[./doc/searching/_index.md][BinarySearch 二分搜索]]
- [[./doc/searching/_index.md][two_sum 两数之和]]
*** Math 数学
- [[./doc/math/_index.md][Complex 复数]]
- [[./doc/math/_index.md][DFT 离散傅立叶变换]]
- [[./doc/math/_index.md][pi 圆周率]]
*** String 字符串
- [[./doc/string/_index.md][MaxSubarray 最大子数组]]
35 changes: 19 additions & 16 deletions src/string/max_substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ pub fn max_substring(s: &str) -> (usize, usize, usize) {
let bytes = s.as_bytes();
let mut char_set = [false; 128]; // 一个简单的Table

let (mut start, mut end, mut max_length) = (0, 0, 0); // 结果
let mut current_start = 0; // 当前不重复子串的开始位置

for i in 0..bytes.len() {
// 如果字符已经出现过,则移动起始位置
while char_set[bytes[i] as usize] == true {
char_set[bytes[current_start] as usize] = false; // 弹出
current_start += 1; // 移动起始位置
let (mut max_left, mut max_right, mut max_length) = (0, 0, 0); // 结果
let mut left = 0; // 当前不重复子串的开始位置

for right in 0..bytes.len() {
while char_set[bytes[right] as usize] == true {
// 拓展后右端字符已经存在
char_set[bytes[left] as usize] = false; // 弹出左端
left += 1; // 右移左端
}

// 标记当前字符为已出现
char_set[bytes[i] as usize] = true;
// 标记右端字符为已出现
char_set[bytes[right] as usize] = true;

// 更新最大长度和结束位置
if i - current_start + 1 > max_length {
max_length = i - current_start + 1;
start = current_start; // 更新起始位置
end = i; // 更新结束位置
if right - left + 1 > max_length {
max_length = right - left + 1;
max_left = left; // 更新起始位置
max_right = right; // 更新结束位置
}
}

// 返回起始位置、结束位置和最大长度
return (start, end, max_length);
return (max_left, max_right, max_length);
}

#[cfg(test)]
Expand All @@ -49,7 +49,10 @@ mod max_substring {
fn test_single_character_string() {
assert_eq!(max_substring("a"), (0, 0, 1));
}

#[test]
fn two_same_characters() {
assert_eq!(max_substring("aa"), (0, 0, 1));
}
#[test]
fn test_all_same_characters() {
assert_eq!(max_substring("aaaaa"), (0, 0, 1));
Expand Down

0 comments on commit 38d19b4

Please sign in to comment.