2154. 将找到的值乘以 2

This commit is contained in:
li-chx 2025-11-19 09:24:56 +08:00
parent fc7f57b9ca
commit 742a5b6137
1 changed files with 8 additions and 45 deletions

View File

@ -1,57 +1,20 @@
use std::collections::HashSet;
use crate::list::{make_list, ListNode};
use std::collections::{HashSet};
mod arr;
mod list;
struct Solution;
impl Solution {
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut set:HashSet<i32> = nums.into_iter().collect();
let mut cur = &head;
while let Some(node) = cur {
if set.contains(&node.val) {
cur = &node.next;
}else {
break;
}
pub fn find_final_value(nums: Vec<i32>, original: i32) -> i32 {
let mut original = original;
let set = nums.into_iter().collect::<HashSet<i32>>();
while set.contains(&original) {
original *= 2;
}
let mut dummy_head = Box::new(ListNode{val:0, next: cur.clone()});
let mut last = &mut dummy_head.next;
// let mut cur = &mut(last.as_mut()?.next);
while let Some(node) = last {
if node.next.is_none() {break;}
if set.contains(&node.next.as_ref().as_ref()?.val) {
// this could be a shit!
let mut next_node = last.as_mut().unwrap().next.as_mut().unwrap().next.take();
let mut st = false;
while let Some(nd) = &next_node {
if set.contains(&nd.as_ref().val) {
next_node = next_node.as_mut().unwrap().next.take();
}else {
st = true;
break;
}
}
if !st {
last.as_mut().unwrap().next = None;
}else {
last.as_mut().unwrap().next = next_node;
}
last = &mut last.as_mut().unwrap().next;
} else {
last = &mut last.as_mut().unwrap().next;
}
}
dummy_head.next
original
}
}
fn main() {
let nums = vec![1];
let head = make_list(vec![1,2,1,2,1,2]);
let result = Solution::modified_list(nums, head);
let result = Solution::find_final_value(vec![5,3,6,1,12],3);
println!("{:?}", result);
}