题解 | #包含min函数的栈#

包含min函数的栈

https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49

using System.Collections.Generic;
class Solution
{
    Stack<int> sta1 = new Stack<int>();
    Stack<int> sta2 = new Stack<int>();
    public void push(int node) 
    {
        sta1.Push(node);
        if(sta2.Count == 0 || node <= sta2.Peek()) sta2.Push(node);
    }
    public void pop() 
    {
        int cur = sta1.Pop();
        if(cur == sta2.Peek()) sta2.Pop();
    }
    public int top() 
    {
        return sta1.Peek();
    }
    public int min() 
    {
        return sta2.Peek();
    }
}

全部评论

相关推荐

06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务