Compare commits

..
2 Commits
3 changed files with 15 additions and 56 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ pub struct ListNode {
impl ListNode { impl ListNode {
#[inline] #[inline]
pub fn new(val: i32) -> Self { fn new(val: i32) -> Self {
ListNode { ListNode {
next: None, next: None,
val val
+13 -42
View File
@@ -1,57 +1,28 @@
use std::collections::HashSet; use std::collections::HashSet;
mod arr; mod arr;
mod list;
struct Solution {} struct Solution {}
impl Solution { impl Solution {
pub fn remove_invalid_parentheses(s: String) -> Vec<String> { pub fn missing_integer(nums: Vec<i32>) -> i32 {
let s = s.as_bytes().to_vec(); let mut st = nums.clone().into_iter().collect::<HashSet<i32>>();
let mut queue1 = HashSet::new(); let mut sum = nums[0];
fn valid(x: &Vec<u8>) -> bool { for i in 1 .. nums.len() {
let mut cnt = 0; if nums[i] != nums[i-1] + 1 {
for c in x { break;
if *c == b'(' {
cnt += 1;
} else if *c == b')' {
cnt -= 1;
if cnt < 0 {
return false;
} }
sum += nums[i];
} }
while st.contains(&sum) {
sum += 1;
} }
cnt == 0 sum
}
queue1.insert(s);
loop {
{
let mut local_ans = vec![];
for x in queue1.iter() {
if valid(&x) {
local_ans.push(x.clone());
}
}
if !local_ans.is_empty() {
return local_ans
.into_iter()
.map(|x| x.iter().map(|&y| y as char).collect::<String>())
.collect();
}
}
let mut queue2 = HashSet::<Vec<u8>>::new();
for x in queue1.into_iter(){
for i in 0..x.len() {
queue2.insert( x[..i].iter().chain(x[i + 1..].iter()).copied().collect());
}
}
queue1 = queue2;
}
} }
} }
fn main() { fn main() {
// 1 2 2 3
// 0 -1 0 1 0
println!( println!(
"{:?}", "{:?}",
Solution::remove_invalid_parentheses(")()())()))(((()()()(()".to_string()) Solution::missing_integer(vec![14,9,6,9,7,9,10,4,9,9,4,4])
); );
} }
+1 -13
View File
@@ -15,19 +15,7 @@ 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;