DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • Array

    • 数组
    • 二分查找
    • moveZeros
  • Dynamic-programming

    • 动态规划
  • 刷题指南
  • String

    • 字符串
  • bitwise-operator

    • 位运算符
  • heap
  • history

    • [1014] 最佳观光组合
    • [1022] 从根到叶的二进制数之和
    • [104] 二叉树的最大深度
    • [11] 盛最多水的容器
    • [110] 平衡二叉树
    • [1227] 飞机座位分配概率
    • [129] 求根节点到叶节点数字之和
    • [1306] 跳跃游戏 III
    • [148] 排序链表
    • 155.最小栈
    • [165] 比较版本号
    • 1763. 最长的美好子字符串
    • [1870] 准时到达的列车最小时速
    • [199] 二叉树的右视图
    • [21] 合并两个有序链表
    • 215.数组中的第 k 个最大元素
    • [2306] 公司命名
    • [234] 回文链表
    • [2516] 每种字符至少取 K 个
    • [316] 去除重复字母
    • [3171] 找到按位或最接近 K 的子数组
    • [322] 零钱兑换
    • [41] 缺失的第一个正数
    • [44] 通配符匹配
    • [494] 目标和
    • [509] 斐波那契数
    • [518] 零钱兑换 II
    • [62] 不同路径
    • [676] 实现一个魔法字典
    • 70 爬楼梯
    • [718] 最长重复子数组
    • [78] 子集
    • [82] 删除排序链表中的重复元素 II
    • [871] 最低加油次数
    • [88] 合并两个有序数组
    • [887] 鸡蛋掉落
    • 958.二叉树的完全性检验
    • [98] 验证二叉搜索树
    • [983] 最低票价
    • leetcode practice
    • 约瑟夫问题
    • 移除重复节点
  • linked-list

    • 706. 设计哈希映射
    • 链表
  • stack

    • stack
  • tree

    • Tree Traversal
    • 二叉树的最近公共祖先
    • 二叉树
    • 题目
  • leetCode 刷题
  • 回溯
  • 排序算法

stack

数组实现

details
    class Stack {
        constructor() {
            this.stack = [];
        }
        push(val) {
            this.stack.push(val);
        }
        peek() {
            if (this.isEmpty()) {
                return null;
            }
            return this.stack.at(-1);
        }
        pop() {
            if (this.isEmpty()) {
                return null;
            }
            return this.stack.pop();
        }
        isEmpty() {
            return this.stack.length === 0;
        }
        size() {
            return this.stack.length;
        }
    }
Rust
    use super::ListNode;

    pub struct Stack<T> {
        stack: Vec<T>,
    }

    impl<T> Stack<T> {
        pub fn new() -> Self {
            Stack { stack: Vec::new() }
        }
        pub fn push(&mut self, val: T) {
            self.stack.push(val);
        }
        pub fn pop(&mut self) -> Option<T> {
            self.stack.pop()
        }
        pub fn peek(&self) -> Option<T>
        where
            T: Copy,
        {
            self.stack.last().copied()
        }
        pub fn is_empty(&self) -> bool {
            self.stack.len() == 0
        }
        pub fn size(&self) -> usize {
            self.stack.len()
        }
    }

链表实现

Javascript
    class ListNode {
        constructor(val, next = null) {
            this.val = val;
            this.next = next;
        }
    }
    class Stack {
        constructor() {
            this.head = null;
            this.size = 0;
        }
        push(val) {
            this.head = new ListNode(val, this.head);
            this.size++;
        }
        peek() {
            if (this.isEmpty()) {
                return null;
            }
            return this.head.val;
        }
        pop() {
            if (this.isEmpty()) {
                return null;
            }
            let node = this.head;
            this.head = this.head.next;
            this.size--;
            return node.val;
        }
        isEmpty() {
            return this.head === null;
        }
        size() {
            return this.size;
        }
    }
Rust
    use super::ListNode;
    use std::{cell::RefCell, rc::Rc};

    pub struct Stack<T> {
        head: Option<Rc<RefCell<ListNode<T>>>>,
        size: usize,
    }

    impl<T> Stack<T> {
        pub fn new() -> Self {
            Self {
                head: None,
                size: 0,
            }
        }
        pub fn push(&mut self, val: T) {
            let node = Rc::new(RefCell::new(ListNode {
                val,
                next: self.head.take(),
            }));
            self.head = Some(node);
            self.size += 1;
        }
        pub fn pop(&mut self) -> Option<T> {
            self.head.take().map(|node| {
                let node = Rc::try_unwrap(node).ok().unwrap().into_inner();
                self.head = node.next;
                self.size -= 1;
                node.val
            })
        }
        pub fn peek(&self) -> Option<T>
        where
            T: Copy,
        {
            self.head.as_ref().map(|node| node.borrow().val)
        }
        pub fn is_empty(&self) -> bool {
            self.head.is_none()
        }
        pub fn size(&self) -> usize {
            self.size
        }
    }
Last Updated:
Contributors: rosendo