Compare commits
2
Commits
master
..
3861dc3a91
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3861dc3a91 | ||
|
|
13ddebfc62 |
+1
-1
@@ -7,7 +7,7 @@ pub struct ListNode {
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
pub fn new(val: i32) -> Self {
|
||||
fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
|
||||
+13
-42
@@ -1,57 +1,28 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
mod arr;
|
||||
mod list;
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn remove_invalid_parentheses(s: String) -> Vec<String> {
|
||||
let s = s.as_bytes().to_vec();
|
||||
let mut queue1 = HashSet::new();
|
||||
fn valid(x: &Vec<u8>) -> bool {
|
||||
let mut cnt = 0;
|
||||
for c in x {
|
||||
if *c == b'(' {
|
||||
cnt += 1;
|
||||
} else if *c == b')' {
|
||||
cnt -= 1;
|
||||
if cnt < 0 {
|
||||
return false;
|
||||
pub fn missing_integer(nums: Vec<i32>) -> i32 {
|
||||
let mut st = nums.clone().into_iter().collect::<HashSet<i32>>();
|
||||
let mut sum = nums[0];
|
||||
for i in 1 .. nums.len() {
|
||||
if nums[i] != nums[i-1] + 1 {
|
||||
break;
|
||||
}
|
||||
sum += nums[i];
|
||||
}
|
||||
while st.contains(&sum) {
|
||||
sum += 1;
|
||||
}
|
||||
cnt == 0
|
||||
}
|
||||
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;
|
||||
}
|
||||
sum
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// 1 2 2 3
|
||||
// 0 -1 0 1 0
|
||||
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
@@ -15,19 +15,7 @@ impl TreeNode {
|
||||
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>>> {
|
||||
if vals.is_empty() || vals[0].is_none() {
|
||||
return None;
|
||||
|
||||
Reference in New Issue
Block a user