716:最大栈
This commit is contained in:
parent
fbac24769a
commit
5010f067de
131
src/main/java/leetcode/editor/cn/MaxStack.java
Normal file
131
src/main/java/leetcode/editor/cn/MaxStack.java
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
//设计一个最大栈数据结构,既支持栈操作,又支持查找栈中最大元素。
|
||||||
|
//
|
||||||
|
// 实现 MaxStack 类:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// MaxStack() 初始化栈对象
|
||||||
|
// void push(int x) 将元素 x 压入栈中。
|
||||||
|
// int pop() 移除栈顶元素并返回这个元素。
|
||||||
|
// int top() 返回栈顶元素,无需移除。
|
||||||
|
// int peekMax() 检索并返回栈中最大元素,无需移除。
|
||||||
|
// int popMax() 检索并返回栈中最大元素,并将其移除。如果有多个最大元素,只要移除 最靠近栈顶 的那个。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入
|
||||||
|
//["MaxStack", "push", "push", "push", "top", "popMax", "top", "peekMax", "pop",
|
||||||
|
// "top"]
|
||||||
|
//[[], [5], [1], [5], [], [], [], [], [], []]
|
||||||
|
//输出
|
||||||
|
//[null, null, null, null, 5, 5, 1, 5, 1, 5]
|
||||||
|
//
|
||||||
|
//解释
|
||||||
|
//MaxStack stk = new MaxStack();
|
||||||
|
//stk.push(5); // [5] - 5 既是栈顶元素,也是最大元素
|
||||||
|
//stk.push(1); // [5, 1] - 栈顶元素是 1,最大元素是 5
|
||||||
|
//stk.push(5); // [5, 1, 5] - 5 既是栈顶元素,也是最大元素
|
||||||
|
//stk.top(); // 返回 5,[5, 1, 5] - 栈没有改变
|
||||||
|
//stk.popMax(); // 返回 5,[5, 1] - 栈发生改变,栈顶元素不再是最大元素
|
||||||
|
//stk.top(); // 返回 1,[5, 1] - 栈没有改变
|
||||||
|
//stk.peekMax(); // 返回 5,[5, 1] - 栈没有改变
|
||||||
|
//stk.pop(); // 返回 1,[5] - 此操作后,5 既是栈顶元素,也是最大元素
|
||||||
|
//stk.top(); // 返回 5,[5] - 栈没有改变
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// -107 <= x <= 107
|
||||||
|
// 最多调用 104 次 push、pop、top、peekMax 和 popMax
|
||||||
|
// 调用 pop、top、peekMax 或 popMax 时,栈中 至少存在一个元素
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 试着设计解决方案:调用 top 方法的时间复杂度为 O(1) ,调用其他方法的时间复杂度为 O(logn) 。
|
||||||
|
//
|
||||||
|
// Related Topics 栈 设计 链表 双向链表 有序集合
|
||||||
|
// 👍 70 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
//716:最大栈
|
||||||
|
public class MaxStack {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
// Solution solution = new MaxStack().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class MaxStack {
|
||||||
|
Stack<Integer> stack;
|
||||||
|
Stack<Integer> max;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initialize your data structure here.
|
||||||
|
*/
|
||||||
|
public MaxStack() {
|
||||||
|
stack = new Stack<>();
|
||||||
|
max = new Stack<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(int x) {
|
||||||
|
stack.push(x);
|
||||||
|
if(max.isEmpty()||x>max.peek()){
|
||||||
|
max.push(x);
|
||||||
|
}else{
|
||||||
|
max.push(max.peek());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int pop() {
|
||||||
|
max.pop();
|
||||||
|
return stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int top() {
|
||||||
|
return stack.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int peekMax() {
|
||||||
|
return max.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int popMax() {
|
||||||
|
int num = max.peek();
|
||||||
|
Stack<Integer> temp = new Stack<>();
|
||||||
|
while (stack.peek()!=num){
|
||||||
|
temp.push(pop());
|
||||||
|
}
|
||||||
|
pop();
|
||||||
|
while (!temp.isEmpty()){
|
||||||
|
push(temp.pop());
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your MaxStack object will be instantiated and called as such:
|
||||||
|
* MaxStack obj = new MaxStack();
|
||||||
|
* obj.push(x);
|
||||||
|
* int param_2 = obj.pop();
|
||||||
|
* int param_3 = obj.top();
|
||||||
|
* int param_4 = obj.peekMax();
|
||||||
|
* int param_5 = obj.popMax();
|
||||||
|
*/
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
55
src/main/java/leetcode/editor/cn/MaxStack.md
Normal file
55
src/main/java/leetcode/editor/cn/MaxStack.md
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<p>设计一个最大栈数据结构,既支持栈操作,又支持查找栈中最大元素。</p>
|
||||||
|
|
||||||
|
<p>实现 <code>MaxStack</code> 类:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>MaxStack()</code> 初始化栈对象</li>
|
||||||
|
<li><code>void push(int x)</code> 将元素 x 压入栈中。</li>
|
||||||
|
<li><code>int pop()</code> 移除栈顶元素并返回这个元素。</li>
|
||||||
|
<li><code>int top()</code> 返回栈顶元素,无需移除。</li>
|
||||||
|
<li><code>int peekMax()</code> 检索并返回栈中最大元素,无需移除。</li>
|
||||||
|
<li><code>int popMax()</code> 检索并返回栈中最大元素,并将其移除。如果有多个最大元素,只要移除 <strong>最靠近栈顶</strong> 的那个。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入</strong>
|
||||||
|
["MaxStack", "push", "push", "push", "top", "popMax", "top", "peekMax", "pop", "top"]
|
||||||
|
[[], [5], [1], [5], [], [], [], [], [], []]
|
||||||
|
<strong>输出</strong>
|
||||||
|
[null, null, null, null, 5, 5, 1, 5, 1, 5]
|
||||||
|
|
||||||
|
<strong>解释</strong>
|
||||||
|
MaxStack stk = new MaxStack();
|
||||||
|
stk.push(5); // [<strong>5</strong>] - 5 既是栈顶元素,也是最大元素
|
||||||
|
stk.push(1); // [<strong>5</strong>, <strong>1</strong>] - 栈顶元素是 1,最大元素是 5
|
||||||
|
stk.push(5); // [5, 1, <strong>5</strong>] - 5 既是栈顶元素,也是最大元素
|
||||||
|
stk.top(); // 返回 5,[5, 1, <strong>5</strong>] - 栈没有改变
|
||||||
|
stk.popMax(); // 返回 5,[<strong>5</strong>, <strong>1</strong>] - 栈发生改变,栈顶元素不再是最大元素
|
||||||
|
stk.top(); // 返回 1,[<strong>5</strong>, <strong>1</strong>] - 栈没有改变
|
||||||
|
stk.peekMax(); // 返回 5,[<strong>5</strong>, <strong>1</strong>] - 栈没有改变
|
||||||
|
stk.pop(); // 返回 1,[<strong>5</strong>] - 此操作后,5 既是栈顶元素,也是最大元素
|
||||||
|
stk.top(); // 返回 5,[<strong>5</strong>] - 栈没有改变
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>-10<sup>7</sup> <= x <= 10<sup>7</sup></code></li>
|
||||||
|
<li>最多调用 <code>10<sup>4</sup></code> 次 <code>push</code>、<code>pop</code>、<code>top</code>、<code>peekMax</code> 和 <code>popMax</code></li>
|
||||||
|
<li>调用 <code>pop</code>、<code>top</code>、<code>peekMax</code> 或 <code>popMax</code> 时,栈中 <strong>至少存在一个元素</strong></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><b>进阶:</b> </p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>试着设计解决方案:调用 <code>top</code> 方法的时间复杂度为 <code>O(1)</code> ,调用其他方法的时间复杂度为 <code>O(logn)</code> 。 </li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>栈</li><li>设计</li><li>链表</li><li>双向链表</li><li>有序集合</li></div></div>\n<div><li>👍 70</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user