155. 最小栈
This commit is contained in:
+70
-21
@@ -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 arr;
|
||||||
mod list;
|
mod list;
|
||||||
mod tree;
|
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;
|
* `&self` means the method takes an immutable reference.
|
||||||
use std::rc::Rc;
|
* If you need a mutable reference, change it to `&mut self` instead.
|
||||||
|
*/
|
||||||
|
impl MinStack {
|
||||||
|
fn new() -> Self {
|
||||||
|
MinStack {
|
||||||
|
heap: BTreeMap::new(),
|
||||||
|
stack: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Solution {
|
fn push(&mut self, value: i32) {
|
||||||
pub fn sum_numbers(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
self.stack.push(value);
|
||||||
Self::dfs(root).iter().map(|x| x.parse::<i32>().unwrap()).sum()
|
let entry = self.heap.entry(Reverse(value)).or_insert(0);
|
||||||
|
*entry += 1;
|
||||||
}
|
}
|
||||||
pub fn dfs(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<String> {
|
|
||||||
if root.is_none() {
|
fn pop(&mut self) {
|
||||||
return vec![];
|
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() {
|
fn main() {
|
||||||
println!(
|
let opers = [
|
||||||
"{:?}",
|
"MinStack", "push", "push", "push", "getMin", "pop", "getMin", "pop", "getMin", "pop",
|
||||||
Solution::sum_numbers(build_tree_from_str("[4,9,0,5,1]"))
|
"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