Compare commits
16
Commits
551db6decf
...
a2f7ac206a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2f7ac206a | ||
|
|
04acafa3ac | ||
|
|
7bfd0c358b | ||
|
|
3d3e79274a | ||
|
|
ec2d2c9be2 | ||
|
|
e48dc20484 | ||
|
|
349a990512 | ||
|
|
d19b5d9ca8 | ||
|
|
7b7fdf87c3 | ||
|
|
d716101d77 | ||
|
|
a813a0b1d6 | ||
|
|
f6368f1307 | ||
|
|
1678a62889 | ||
|
|
d167038744 | ||
|
|
1c46d51b5f | ||
|
|
5818811a53 |
+1
-1
@@ -7,7 +7,7 @@ pub struct ListNode {
|
|||||||
|
|
||||||
impl ListNode {
|
impl ListNode {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn new(val: i32) -> Self {
|
pub fn new(val: i32) -> Self {
|
||||||
ListNode {
|
ListNode {
|
||||||
next: None,
|
next: None,
|
||||||
val
|
val
|
||||||
|
|||||||
+53
-16
@@ -1,29 +1,66 @@
|
|||||||
|
use crate::arr::make_char_matrix;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
|
mod list;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn min_distance(word1: String, word2: String) -> i32 {
|
pub fn has_valid_path(grid: Vec<Vec<char>>) -> bool {
|
||||||
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
|
if grid[0][0] == ')'
|
||||||
let word1 = word1.chars().collect::<Vec<char>>();
|
|| grid[grid.len() - 1][grid[0].len() - 1] == '('
|
||||||
let word2 = word2.chars().collect::<Vec<char>>();
|
|| (grid.len() + grid[0].len()) % 2 == 0
|
||||||
for i in 1..=word1.len() {
|
{
|
||||||
dp[i][0] = dp[i - 1][0] + 1;
|
return false;
|
||||||
}
|
}
|
||||||
for i in 1..=word2.len() {
|
let mut matrix = vec![vec![Option::<HashSet<i32>>::None; grid[0].len()]; grid.len()];
|
||||||
dp[0][i] = dp[0][i - 1] + 1;
|
matrix[0][0] = Some(HashSet::from_iter([10000]));
|
||||||
}
|
for i in 0..=(grid.len() - 1 + grid[0].len() - 1) {
|
||||||
for i in 0..word1.len() {
|
for j in 0..=i {
|
||||||
for j in 0..word2.len() {
|
if grid.len() > j && grid[0].len() > i - j {
|
||||||
dp[i + 1][j + 1] = (dp[i][j] + if word1[i] != word2[j] { 1 } else { 0 })
|
if grid.len() > j + 1 {
|
||||||
.min(dp[i][j + 1] + 1)
|
let (rsrc, rdst) = matrix.split_at_mut(j + 1);
|
||||||
.min(dp[i + 1][j] + 1);
|
let dst = rdst[0][i - j].get_or_insert_with(HashSet::new);
|
||||||
|
let src = rsrc[j][i - j].as_ref().unwrap();
|
||||||
|
if grid[j + 1][i - j] == '(' {
|
||||||
|
dst.extend(src.iter().map(|&x| x + 10000));
|
||||||
|
} else {
|
||||||
|
dst.extend(
|
||||||
|
src.iter()
|
||||||
|
.filter(|&x| x / 10000 > x % 10000)
|
||||||
|
.map(|&x| x + 1),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dp[word1.len()][word2.len()]
|
if grid[0].len() > i - j + 1 {
|
||||||
|
let (rsrc, rdst) = matrix[j].split_at_mut(i - j + 1);
|
||||||
|
let dst = rdst[0].get_or_insert_with(HashSet::new);
|
||||||
|
let src = rsrc[i - j].as_ref().unwrap();
|
||||||
|
if grid[j][i - j + 1] == '(' {
|
||||||
|
dst.extend(src.iter().map(|&x| x + 10000));
|
||||||
|
} else {
|
||||||
|
dst.extend(
|
||||||
|
src.iter()
|
||||||
|
.filter(|&x| x / 10000 > x % 10000)
|
||||||
|
.map(|&x| x + 1),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
matrix[grid.len() - 1][grid[0].len() - 1].as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.any(|&x| x / 10000 == x % 10000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!(
|
||||||
"{:?}",
|
"{:?}",
|
||||||
Solution::min_distance("intention".to_string(), "execution".to_string())
|
Solution::has_valid_path(make_char_matrix(
|
||||||
|
r#"[["(","(","("],[")","(",")"],["(","(",")"],["(","(",")"]]"#
|
||||||
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-1
@@ -15,7 +15,19 @@ impl TreeNode {
|
|||||||
TreeNode { val, left: None, right: None }
|
TreeNode { val, left: None, right: None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn build_tree_from_str(s: &str) -> Option<Rc<RefCell<TreeNode>>> {
|
||||||
|
// [-10,9,20,null,null,15,7]
|
||||||
|
let s = s.trim_matches(&['[', ']'][..]).split(',').map(|x| x.trim()).collect::<Vec<_>>();
|
||||||
|
let mut vals = vec![];
|
||||||
|
for x in s {
|
||||||
|
if x == "null" {
|
||||||
|
vals.push(None);
|
||||||
|
}else {
|
||||||
|
vals.push(Some(i32::from_str_radix(x, 10).unwrap()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
build_tree(&vals)
|
||||||
|
}
|
||||||
pub fn build_tree(vals: &[Option<i32>]) -> Option<Rc<RefCell<TreeNode>>> {
|
pub fn build_tree(vals: &[Option<i32>]) -> Option<Rc<RefCell<TreeNode>>> {
|
||||||
if vals.is_empty() || vals[0].is_none() {
|
if vals.is_empty() || vals[0].is_none() {
|
||||||
return None;
|
return None;
|
||||||
|
|||||||
Reference in New Issue
Block a user