-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 第四章 字符串part01 | ||
|
||
今日任务 | ||
|
||
● 344.反转字符串 | ||
● 541. 反转字符串II | ||
● 剑指Offer 05.替换空格 | ||
● 151.翻转字符串里的单词 | ||
● 剑指Offer58-II.左旋转字符串 | ||
|
||
详细布置 | ||
|
||
## 344.反转字符串 | ||
|
||
建议: 本题是字符串基础题目,就是考察 reverse 函数的实现,同时也明确一下 平时刷题什么时候用 库函数,什么时候 不用库函数 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0344.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.html | ||
|
||
## 541. 反转字符串II | ||
|
||
建议:本题又进阶了,自己先去独立做一做,然后在看题解,对代码技巧会有很深的体会。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0541.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2II.html | ||
|
||
|
||
## 剑指Offer 05.替换空格 | ||
|
||
建议:对于线性数据结构,填充或者删除,后序处理会高效的多。好好体会一下。 | ||
题目链接/文章讲解:https://programmercarl.com/%E5%89%91%E6%8C%87Offer05.%E6%9B%BF%E6%8D%A2%E7%A9%BA%E6%A0%BC.html | ||
|
||
|
||
## 151.翻转字符串里的单词 | ||
|
||
建议:这道题目基本把 刚刚做过的字符串操作 都覆盖了,不过就算知道解题思路,本题代码并不容易写,要多练一练。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0151.%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%87%8C%E7%9A%84%E5%8D%95%E8%AF%8D.html | ||
|
||
## 剑指Offer58-II.左旋转字符串 | ||
|
||
建议:题解中的解法如果没接触过的话,应该会想不到 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/%E5%89%91%E6%8C%87Offer58-II.%E5%B7%A6%E6%97%8B%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.html | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 151. 反转字符串中的单词 | ||
|
||
## 题目描述 | ||
给你一个字符串 s ,请你反转字符串中 单词 的顺序。 | ||
|
||
单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。 | ||
|
||
返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。 | ||
|
||
注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。 | ||
|
||
|
||
## 解题思路 | ||
|
||
不能用库函数自己写的话还是有点烦,但是不难 | ||
|
||
```rust | ||
|
||
struct Solution {} | ||
|
||
impl Solution { | ||
// O(1) space O(n) time | ||
pub fn reverse_words(s: String) -> String { | ||
let mut v: Vec<char> = vec![]; | ||
for c in s.chars().rev() { | ||
if c == ' ' { | ||
let a = v.last(); | ||
if a.is_none() { continue } | ||
if *a.unwrap() == ' ' { continue } | ||
} | ||
v.push(c); | ||
} | ||
if v[v.len() - 1] == ' ' { v.pop(); } | ||
// reverse each word | ||
let mut start = 0; | ||
while start < v.len() { | ||
let mut end = start + 1; | ||
while end < v.len() && v[end] != ' ' { end += 1 } | ||
Self::reverse(&mut v[start..end]); | ||
start = end + 1; | ||
} | ||
v.iter().collect() | ||
} | ||
|
||
pub fn reverse(s: &mut [char]) { | ||
let n = s.len(); | ||
for i in 0..n/2 { | ||
let tmp = s[i]; | ||
s[i] = s[n-1-i]; | ||
s[n-1-i] = tmp; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 344. 反转字符串 | ||
|
||
## 题目描述 | ||
|
||
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。 | ||
|
||
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
|
||
struct Solution {} | ||
impl Solution { | ||
pub fn reverse_string(s: &mut Vec<char>) { | ||
let n = s.len(); | ||
for i in 0..n/2 { | ||
let tmp = s[i]; | ||
s[i] = s[n-1-i]; | ||
s[n-1-i] = tmp; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 541. 反转字符串 II | ||
|
||
## 题目描述 | ||
|
||
给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。 | ||
|
||
如果剩余字符少于 k 个,则将剩余字符全部反转。 | ||
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
|
||
struct Solution {} | ||
impl Solution { | ||
pub fn reverse_str(s: String, k: i32) -> String { | ||
let s = s.chars().collect::<Vec<char>>(); | ||
let mut v: Vec<char> = vec![]; | ||
let mut rev = true; | ||
for i in s.chunks(k as usize) { | ||
if rev { | ||
v.extend(i.iter().rev()); | ||
} else { | ||
v.extend(i); | ||
} | ||
rev = !rev; | ||
} | ||
v.iter().collect() | ||
} | ||
} | ||
``` | ||
|
||
type system好优雅,真的优雅,真的好优雅, | ||
|
||
slice是实现`std::iter::DoubleEndedIterator`的,所以可以reverse,iter返回的不是不同的迭代器而是`std::slice::Iter` | ||
|
||
Vec是实现`Extend` trait的,所以可以用slice的`copy_from_slice`实现extend | ||
|
||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# 剑指 Offer 05. 替换空格 | ||
|
||
## 题目描述 | ||
|
||
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 | ||
|
||
## 解题思路 | ||
|
||
一开始没有想到用双指针 | ||
|
||
```rust | ||
struct Solution {} | ||
impl Solution { | ||
pub fn replace_space(s: String) -> String { | ||
let mut s = s.chars().collect::<Vec<char>>(); | ||
let cnt = s.iter().filter(|&&x| x == ' ').count(); | ||
let n = s.len(); | ||
s.resize(n + 2 * cnt, ' '); | ||
let mut tail = s.len() - 1; | ||
let mut head = n as isize - 1; | ||
while head >= 0 { | ||
if s[head as usize] == ' ' { | ||
s[tail] = '0'; | ||
s[tail - 1] = '2'; | ||
s[tail - 2] = '%'; | ||
tail -= 3; | ||
} else { | ||
s[tail] = s[head as usize]; | ||
tail -= 1; | ||
} | ||
head -= 1; | ||
} | ||
s.iter().collect() | ||
} | ||
} | ||
``` | ||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 剑指 Offer 58 - II. 左旋转字符串 | ||
|
||
## 题目描述 | ||
|
||
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。 | ||
|
||
## 解题思路 | ||
|
||
想不到的思路 | ||
|
||
```rust | ||
|
||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn reverse_left_words(s: String, n: i32) -> String { | ||
let mut v: Vec<char> = s.chars().collect(); | ||
Self::reverse2(&mut v[..n as usize]); | ||
Self::reverse2(&mut v[n as usize..]); | ||
Self::reverse2(&mut v[..]); | ||
v.iter().collect() | ||
} | ||
|
||
pub fn reverse2(s: &mut [char]) { | ||
let n = s.len(); | ||
for i in 0..n/2 { | ||
let tmp = s[i]; | ||
s[i] = s[n-1-i]; | ||
s[n-1-i] = tmp; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 |