Skip to content

Commit a647cef

Browse files
committed
1
1 parent 44f1eeb commit a647cef

File tree

6 files changed

+136
-6
lines changed

6 files changed

+136
-6
lines changed

notes/src/day7/lc15.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,43 @@ impl Solution {
4949

5050
双指针做法还是想不到
5151

52-
## 学习感想
52+
## 学习感想
53+
54+
55+
56+
想不到用双指针
57+
58+
```rust
59+
60+
struct Solution {}
61+
62+
impl Solution {
63+
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
64+
nums.sort();
65+
let mut res: Vec<Vec<i32>> = Vec::new();
66+
for i in 0..nums.len() - 2usize {
67+
if i > 0 && nums[i] == nums[i-1usize] { continue }
68+
let mut left: usize = i + 1usize;
69+
let mut right: usize = nums.len() - 1usize;
70+
use std::cmp::Ordering;
71+
while left < right {
72+
let s: i32 = nums[i] + nums[left] + nums[right];
73+
match s.cmp(&0i32) {
74+
Ordering::Less => left += 1usize,
75+
Ordering::Greater => right -= 1usize,
76+
_ => {
77+
let to_push: Vec<i32> = vec![nums[i], nums[left], nums[right]];
78+
if res.is_empty() || Some(&to_push) != res.last() {
79+
res.push(to_push);
80+
}
81+
left += 1usize;
82+
right -= 1usize;
83+
}
84+
}
85+
}
86+
}
87+
res
88+
}
89+
}
90+
91+
```

notes/src/day7/lc454.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl Solution {
7979

8080

8181

82-
82+
地一下子还是没有想到要用hashmap做,感觉可能是双指针
8383

8484
```rust
8585
struct Solution {}

notes/src/day8/lc151.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,17 @@ impl Solution {
5353
}
5454
```
5555

56-
## 学习感想
56+
## 学习感想
57+
58+
59+
```rust
60+
61+
struct Solution {}
62+
impl Solution {
63+
pub fn reverse_words(s: String) -> String {
64+
s.split_ascii_whitespace().rev().collect::<Vec<&str>>().join(" ")
65+
// ^ String -> &str
66+
// ^ iter Target=&str ^ Vec<&str>
67+
}
68+
}
69+
```

notes/src/day8/lc541.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ slice是实现`std::iter::DoubleEndedIterator`的,所以可以reverse,iter
3636

3737
Vec是实现`Extend` trait的,所以可以用slice的`copy_from_slice`实现extend
3838

39-
## 学习感想
39+
## 学习感想
40+
41+
```rust
42+
43+
struct Solution {}
44+
impl Solution {
45+
pub fn reverse_str(s: String, k: i32) -> String {
46+
let mut s: Vec<u8> = s.bytes().collect();
47+
let mut i: usize = 0usize;
48+
loop {
49+
let start: usize = i * k as usize * 2usize;
50+
if start >= s.len() { break }
51+
i += 1usize;
52+
let end: usize = std::cmp::min(start + k as usize, s.len());
53+
s[start .. end].reverse()
54+
}
55+
String::from_utf8(s).unwrap()
56+
}
57+
}
58+
```

notes/src/day9/lc28.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,20 @@ impl Solution {
9090
```
9191
## 学习感想
9292

93-
还得学习复习
93+
还得学习复习
94+
95+
96+
97+
```rust
98+
struct Solution {}
99+
impl Solution {
100+
pub fn str_str(haystack: String, needle: String) -> i32 {
101+
if let Some(x) = haystack.find(&needle) {
102+
x as i32
103+
} else {
104+
-1i32
105+
}
106+
}
107+
}
108+
109+
```

notes/src/day9/lc459.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ abacabacabac
2020
2121
abac
2222
0010
23+
24+
abcabc
25+
000123
2326
```
2427

2528
```rust
@@ -53,4 +56,44 @@ impl Solution {
5356

5457
## 学习感想
5558

56-
什么时候该用KMP很懵
59+
什么时候该用KMP很懵
60+
61+
next 数组 -- 前缀表 内容是什么??next[i] 记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀
62+
63+
前缀表是用来回退的,它记录了模式串P与主串(文本串)不匹配的时候,模式串应该从哪里开始重新匹配。
64+
65+
记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。
66+
67+
68+
记住例子
69+
70+
```
71+
abcdabcd
72+
00001234
73+
```
74+
75+
76+
```rust
77+
impl Solution {
78+
pub fn repeated_substring_pattern(s: String) -> bool {
79+
let s: Vec<u8> = s.bytes().collect();
80+
// next[i] -> max length of common prefix & suffix of string s[0..=i]
81+
let mut next: Vec<usize> = vec![0usize; s.len()];
82+
let mut left: usize = 0usize; // the current max length of common pre/suf-fix
83+
for right in 1usize .. next.len() { // calculate each next[right]
84+
while left > 1usize && s[right] != s[left] {
85+
left = next[left - 1usize];
86+
}
87+
if s[right] == s[left] {
88+
left += 1usize;
89+
}
90+
next[right] = left;
91+
}
92+
let x: usize = s.len() - *next.last().unwrap();
93+
match s.len() % x {
94+
0 => true,
95+
_ => false,
96+
}
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)