3217. 从链表中移除在数组中存在的节点 ps: Rust写数据结构还是太权威了
This commit is contained in:
parent
941d05d9ee
commit
fc7f57b9ca
|
|
@ -0,0 +1,33 @@
|
||||||
|
// Definition for singly-linked list.
|
||||||
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||||
|
pub struct ListNode {
|
||||||
|
pub val: i32,
|
||||||
|
pub next: Option<Box<ListNode>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ListNode {
|
||||||
|
#[inline]
|
||||||
|
fn new(val: i32) -> Self {
|
||||||
|
ListNode {
|
||||||
|
next: None,
|
||||||
|
val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn make_list(arr: Vec<i32>) -> Option<Box<ListNode>> {
|
||||||
|
let mut head: Option<Box<ListNode>> = None;
|
||||||
|
let mut current = &mut head;
|
||||||
|
|
||||||
|
for &value in arr.iter() {
|
||||||
|
let new_node = Box::new(ListNode::new(value));
|
||||||
|
if current.is_none() {
|
||||||
|
*current = Some(new_node);
|
||||||
|
} else {
|
||||||
|
current.as_mut().unwrap().next = Some(new_node);
|
||||||
|
current = &mut current.as_mut().unwrap().next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
head
|
||||||
|
}
|
||||||
131
src/main.rs
131
src/main.rs
|
|
@ -1,106 +1,57 @@
|
||||||
use std::cmp::min;
|
use std::collections::HashSet;
|
||||||
use std::collections::{BTreeSet, HashMap};
|
use crate::list::{make_list, ListNode};
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
|
mod list;
|
||||||
#[derive(Debug)]
|
|
||||||
struct Pair {
|
|
||||||
index: i32,
|
|
||||||
value: i32,
|
|
||||||
}
|
|
||||||
impl PartialEq for Pair {
|
|
||||||
fn eq(&self, other: &Pair) -> bool {
|
|
||||||
self.value == other.value && self.index == other.index
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Eq for Pair {}
|
|
||||||
impl PartialOrd for Pair {
|
|
||||||
fn partial_cmp(&self, other: &Pair) -> Option<std::cmp::Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Ord for Pair {
|
|
||||||
fn cmp(&self, other: &Pair) -> std::cmp::Ordering {
|
|
||||||
if self.value == other.value {
|
|
||||||
other.index.cmp(&self.index)
|
|
||||||
} else {
|
|
||||||
other.value.cmp(&self.value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clone for Pair {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Pair {
|
|
||||||
index: self.index,
|
|
||||||
value: self.value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Solution;
|
struct Solution;
|
||||||
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn find_x_sum(nums: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
|
pub fn modified_list(nums: Vec<i32>, head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
|
||||||
let x = x as usize;
|
let mut set:HashSet<i32> = nums.into_iter().collect();
|
||||||
let mut tree: BTreeSet<Pair> = BTreeSet::new();
|
let mut cur = &head;
|
||||||
let mut dict: HashMap<i32, Pair> = HashMap::new();
|
while let Some(node) = cur {
|
||||||
let mut ans: Vec<i32> = vec![];
|
if set.contains(&node.val) {
|
||||||
for i in 0..k as usize {
|
cur = &node.next;
|
||||||
let mut pair = Pair {
|
}else {
|
||||||
index: nums[i],
|
break;
|
||||||
value: 1,
|
|
||||||
};
|
|
||||||
if dict.contains_key(&nums[i]) {
|
|
||||||
let exist_pair = dict.get(&nums[i]).unwrap();
|
|
||||||
tree.remove(exist_pair);
|
|
||||||
pair.value += exist_pair.value;
|
|
||||||
}
|
}
|
||||||
dict.insert(nums[i], pair.clone());
|
|
||||||
tree.insert(pair);
|
|
||||||
}
|
}
|
||||||
ans.push(
|
let mut dummy_head = Box::new(ListNode{val:0, next: cur.clone()});
|
||||||
tree.iter()
|
let mut last = &mut dummy_head.next;
|
||||||
.take(min(tree.len(), x))
|
|
||||||
.fold(0, |last, cur| last + cur.value * cur.index),
|
// let mut cur = &mut(last.as_mut()?.next);
|
||||||
);
|
while let Some(node) = last {
|
||||||
for i in 0..nums.len() - k as usize {
|
if node.next.is_none() {break;}
|
||||||
let mut left_item = dict[&nums[i]].clone();
|
if set.contains(&node.next.as_ref().as_ref()?.val) {
|
||||||
tree.remove(&left_item);
|
// this could be a shit!
|
||||||
if left_item.value > 1 {
|
let mut next_node = last.as_mut().unwrap().next.as_mut().unwrap().next.take();
|
||||||
left_item.value -= 1;
|
let mut st = false;
|
||||||
dict.insert(nums[i], left_item.clone());
|
while let Some(nd) = &next_node {
|
||||||
tree.insert(left_item);
|
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 {
|
} else {
|
||||||
dict.remove(&nums[i]);
|
last = &mut last.as_mut().unwrap().next;
|
||||||
}
|
}
|
||||||
if dict.contains_key(&nums[i + k as usize]) {
|
|
||||||
let mut pair = dict.get(&nums[i + k as usize]).unwrap().clone();
|
|
||||||
tree.remove(&pair);
|
|
||||||
pair.value += 1;
|
|
||||||
dict.insert(nums[i + k as usize], pair.clone());
|
|
||||||
tree.insert(pair);
|
|
||||||
} else {
|
|
||||||
let pair = Pair {
|
|
||||||
index: nums[i + k as usize],
|
|
||||||
value: 1,
|
|
||||||
};
|
|
||||||
dict.insert(nums[i + k as usize], pair.clone());
|
|
||||||
tree.insert(pair);
|
|
||||||
};
|
|
||||||
ans.push(
|
|
||||||
tree.iter()
|
|
||||||
.take(min(tree.len(), x))
|
|
||||||
.fold(0, |last, cur| last + cur.value * cur.index),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
ans
|
dummy_head.next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let nums = vec![1, 1, 2, 2, 3, 4, 2, 3];
|
let nums = vec![1];
|
||||||
let k = 6;
|
let head = make_list(vec![1,2,1,2,1,2]);
|
||||||
let x = 2;
|
let result = Solution::modified_list(nums, head);
|
||||||
let result = Solution::find_x_sum(nums, k, x);
|
|
||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue