155. 最小栈
This commit is contained in:
+72
-23
@@ -1,35 +1,84 @@
|
||||
use crate::list::{ListNode, make_list};
|
||||
use crate::arr::make_matrix;
|
||||
use std::cmp::Reverse;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
mod arr;
|
||||
mod list;
|
||||
mod tree;
|
||||
|
||||
struct Solution {}
|
||||
struct MinStack {
|
||||
heap: BTreeMap<Reverse<i32>, i32>,
|
||||
stack: Vec<i32>,
|
||||
}
|
||||
|
||||
use crate::tree::{TreeNode, build_tree_from_str};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
impl Solution {
|
||||
pub fn sum_numbers(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
Self::dfs(root).iter().map(|x| x.parse::<i32>().unwrap()).sum()
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl MinStack {
|
||||
fn new() -> Self {
|
||||
MinStack {
|
||||
heap: BTreeMap::new(),
|
||||
stack: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn dfs(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<String> {
|
||||
if root.is_none() {
|
||||
return vec![];
|
||||
|
||||
fn push(&mut self, value: i32) {
|
||||
self.stack.push(value);
|
||||
let entry = self.heap.entry(Reverse(value)).or_insert(0);
|
||||
*entry += 1;
|
||||
}
|
||||
|
||||
fn pop(&mut self) {
|
||||
let ele = self.stack.pop();
|
||||
let entry = self.heap.get_mut(&Reverse(ele.unwrap())).unwrap();
|
||||
if *entry == 1 {
|
||||
self.heap.remove_entry(&Reverse(ele.unwrap()));
|
||||
} else {
|
||||
*entry -= 1;
|
||||
}
|
||||
let x = root.as_ref().unwrap();
|
||||
let mut arr = Self::dfs(Rc::clone(x).borrow().left.clone());
|
||||
arr.append(&mut Self::dfs(Rc::clone(x).borrow().right.clone()));
|
||||
if arr.is_empty() {
|
||||
return vec![x.borrow().val.to_string()];
|
||||
}
|
||||
arr.iter().map(|y| x.borrow().val.to_string() + y).collect()
|
||||
}
|
||||
|
||||
fn top(&self) -> i32 {
|
||||
self.stack.last().cloned().unwrap()
|
||||
}
|
||||
|
||||
fn get_min(&self) -> i32 {
|
||||
self.heap.last_key_value().unwrap().0.0
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your MinStack object will be instantiated and called as such:
|
||||
* let obj = MinStack::new();
|
||||
* obj.push(value);
|
||||
* obj.pop();
|
||||
* let ret_3: i32 = obj.top();
|
||||
* let ret_4: i32 = obj.get_min();
|
||||
*/
|
||||
fn main() {
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::sum_numbers(build_tree_from_str("[4,9,0,5,1]"))
|
||||
);
|
||||
let opers = [
|
||||
"MinStack", "push", "push", "push", "getMin", "pop", "getMin", "pop", "getMin", "pop",
|
||||
"push", "push", "push", "getMin", "pop", "top", "getMin", "pop", "getMin", "pop",
|
||||
];
|
||||
let data =
|
||||
make_matrix("[[],[0],[1],[0],[],[],[],[],[],[],[-2],[-1],[-2],[],[],[],[],[],[],[]]");
|
||||
let mut st = MinStack::new();
|
||||
for i in 1..opers.len() {
|
||||
match opers[i] {
|
||||
"push" => {
|
||||
st.push(data[i][0]);
|
||||
}
|
||||
"pop" => {
|
||||
st.pop();
|
||||
}
|
||||
"top" => {
|
||||
println!("{:?}", st.top());
|
||||
}
|
||||
"getMin" => {
|
||||
println!("{:?}", st.get_min());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user