76. 最小覆盖子串
This commit is contained in:
+50
-17
@@ -1,31 +1,64 @@
|
||||
mod arr;
|
||||
mod tree;
|
||||
|
||||
use crate::tree::{TreeNode, build_tree_from_str};
|
||||
struct Solution {}
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
impl Solution {
|
||||
pub fn right_side_view(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result = vec![];
|
||||
Self::dfs(root, &mut result,0);
|
||||
result
|
||||
}
|
||||
fn dfs(root: Option<Rc<RefCell<TreeNode>>>, mut arr: &mut Vec<i32>,depth: usize) {
|
||||
if root.is_none() {
|
||||
return;
|
||||
pub fn min_window(s: String, t: String) -> String {
|
||||
let s = s.chars().collect::<Vec<char>>();
|
||||
let t = t.chars().collect::<Vec<char>>();
|
||||
if t.len() == 1 && s[0] == t[0] {
|
||||
return t.iter().collect();
|
||||
}
|
||||
while arr.len() <= depth {
|
||||
arr.push(0);
|
||||
let mut arr2 = vec![0; 123];
|
||||
let mut arr1 = vec![0; 123];
|
||||
// 65-90 97-122
|
||||
//return "".to_string();
|
||||
for c in t.iter() {
|
||||
arr2[*c as usize] += 1;
|
||||
}
|
||||
(*arr)[depth] = root.as_ref().unwrap().borrow().val;
|
||||
Self::dfs(root.as_ref().unwrap().borrow().left.clone(), &mut arr,depth + 1);
|
||||
Self::dfs(root.as_ref().unwrap().borrow().right.clone(), &mut arr,depth + 1);
|
||||
// let (mut l, mut r)= (-1, -1)
|
||||
let mut ans = 10000000;
|
||||
let mut ans_index = 100000000;
|
||||
let mut l = 0;
|
||||
let mut is_find_ans = false;
|
||||
for r in 0..s.len() {
|
||||
let cur = s[r] as usize;
|
||||
arr1[cur] += 1;
|
||||
if (is_find_ans && s[l] as usize == cur || !is_find_ans) && arr1[cur] >= arr2[cur] {
|
||||
if || -> bool {
|
||||
for i in 65..=90 {
|
||||
if arr1[i] < arr2[i] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for i in 97..=122 {
|
||||
if arr1[i] < arr2[i] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}() {
|
||||
while arr1[s[l] as usize] > arr2[s[l] as usize] {
|
||||
arr1[s[l] as usize] -= 1;
|
||||
l += 1;
|
||||
}
|
||||
is_find_ans = true;
|
||||
if r - l + 1 < ans {
|
||||
ans = r - l + 1;
|
||||
ans_index = l;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ans_index == 100000000 {
|
||||
return "".to_string();
|
||||
}
|
||||
s[ans_index..ans_index + ans].iter().collect()
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::right_side_view(build_tree_from_str("[1,2,3,4,null,null,null,5]"))
|
||||
Solution::min_window("cabwefgewcwaefgcf".to_string(), "cae".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user