We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/min-stack/
解题思路:
本题解参考了官方题解。
复杂度分析:
时间复杂度:各操作均为O(1) 空间复杂度:O(n)
/** * initialize your data structure here. */ var MinStack = function () { this.stack = []; this.minStack = [Infinity]; // 使用无穷大,保证第一个值入栈时,一定会被存入辅助栈 }; /** * @param {number} x * @return {void} */ MinStack.prototype.push = function (x) { this.stack.push(x); // 每次存入一个值时,都将当前最小值与存入的值进行比较,保存较小的值。 // 可以理解为,缓存了每次入栈操作时,当前栈的最小值。 this.minStack.push(Math.min(this.minStack[this.minStack.length - 1], x)); }; /** * @return {void} */ MinStack.prototype.pop = function () { this.stack.pop(); this.minStack.pop(); }; /** * @return {number} */ MinStack.prototype.top = function () { return this.stack[this.stack.length - 1]; }; /** * @return {number} */ MinStack.prototype.getMin = function () { return this.minStack[this.minStack.length - 1]; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/min-stack/
解题思路:
本题解参考了官方题解。
复杂度分析:
时间复杂度:各操作均为O(1)
空间复杂度:O(n)
The text was updated successfully, but these errors were encountered: