LeetCode-155. Min Stack

网友投稿 593 2022-10-03

LeetCode-155. Min Stack

LeetCode-155. Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack-() -- Get the top element.getMin() -- Retrieve the minimum element in the stack.

Example:

MinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.getMin(); --> Returns -3.minStack.pop();minStack-(); --> Returns 0.minStack.getMin(); --> Returns -2.

题解:

class MinStack {public: stack m; stack s; /** initialize your data structure here. */ MinStack() { } void push(int x) { s.push(x); if (m.empty() == true) { m.push(x); } else { int t = m-(); if (t < x) { m.push(t); } else { m.push(x); } } } void pop() { s.pop(); m.pop(); } int top() { return s-(); } int getMin() { return m-(); }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序可以不添加开发者吗(微信小程序添加开发者是什么)
下一篇:golang实现ecc密钥对与[]byte类型转换
相关文章

 发表评论

暂时没有评论,来抢沙发吧~