File tree Expand file tree Collapse file tree 6 files changed +136
-6
lines changed Expand file tree Collapse file tree 6 files changed +136
-6
lines changed Original file line number Diff line number Diff line change @@ -49,4 +49,43 @@ impl Solution {
49
49
50
50
双指针做法还是想不到
51
51
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
+ ```
Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ impl Solution {
79
79
80
80
81
81
82
-
82
+ 地一下子还是没有想到要用hashmap做,感觉可能是双指针
83
83
84
84
``` rust
85
85
struct Solution {}
Original file line number Diff line number Diff line change @@ -53,4 +53,17 @@ impl Solution {
53
53
}
54
54
```
55
55
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
+ ```
Original file line number Diff line number Diff line change @@ -36,4 +36,23 @@ slice是实现`std::iter::DoubleEndedIterator`的,所以可以reverse,iter
36
36
37
37
Vec是实现` Extend ` trait的,所以可以用slice的` copy_from_slice ` 实现extend
38
38
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
+ ```
Original file line number Diff line number Diff line change @@ -90,4 +90,20 @@ impl Solution {
90
90
```
91
91
## 学习感想
92
92
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
+ ```
Original file line number Diff line number Diff line change @@ -20,6 +20,9 @@ abacabacabac
20
20
21
21
abac
22
22
0010
23
+
24
+ abcabc
25
+ 000123
23
26
```
24
27
25
28
``` rust
@@ -53,4 +56,44 @@ impl Solution {
53
56
54
57
## 学习感想
55
58
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
+ ```
You can’t perform that action at this time.
0 commit comments