Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainevsia committed Jul 20, 2023
1 parent 9eb4801 commit 4626a44
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions 704. 二分查找/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
fn main() {
let a = Solution::maximum_beauty(vec![49,26], 12);
let a = Solution::str_str("hello".into(), "ll".into());
dbg!(a);
let a = 1i32;
let b = 2i32;
let c = 1i32 as usize + a as usize == b as usize;
dbg!(c);;
}

struct Solution {}

impl Solution {
pub fn repeated_substring_pattern(s: String) -> bool {
let n = s.len();
let mut next = vec![0; n];
let s = s.chars().collect::<Vec<char>>();
let mut j = 0;
for i in 1..n {
while j >= 1 && s[i] != s[j] {
j = next[j - 1];
}
if s[i] == s[j] {
j += 1;
}
next[i] = j; // next 表示以i结尾的字符串最大前后缀长度
}
let a = *next.last().unwrap();
if a == 0 { return false }
let b = n - a;
if n % b != 0 { return false }
else { true }
}
}

impl Solution {
pub fn str_str(haystack: String, needle: String) -> i32 {
let n = needle.len();
let mut next = vec![0; n];
let hay = haystack.chars().collect::<Vec<char>>();
let s = needle.chars().collect::<Vec<char>>();
let mut j = 0;
for i in 1..n {
while j >= 1 && s[i] != s[j] {
j = next[j - 1];
}
if s[i] == s[j] {
j += 1;
}
next[i] = j; // next 表示以i结尾的字符串最大前后缀长度
}
// dbg!(&next);
// build next ok
if n == 0 { return 0 }
j = 0;
for i in 0..hay.len() {
// dbg!(i, j);
while j >= 1 && hay[i] != s[j] {
j = next[j - 1];
}
if hay[i] == s[j] {
j += 1;
}
if j == n {
return (i + 1 - n) as i32
}
}
-1
}
}

impl Solution {
pub fn reverse_left_words(s: String, n: i32) -> String {
let mut v: Vec<char> = s.chars().collect();
Expand Down

0 comments on commit 4626a44

Please sign in to comment.