Leetcode#155Min Stack

Min Stack

Total Accepted: 28489 Total Submissions: 160974My Submissions

Question Solution

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.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

分析,我使用双向链表,把栈中值按大小关系,连接起来

class MinStack {

element Head=new element(0);

List<element> stack=new ArrayList<element>();

int top=0;

public void push(int x) {

element y=new element(x);

top++;

stack.add(y);

element z=Head;

while(z.next!=null&&x>z.next.x)

{

z=z.next;

}

if(z.next==null)

{

z.next=y;

y.last=z;

}

else

{

y.next=z.next;

z.next=y;

y.next.last=y;

y.last=z;

}

}

public void pop() {

top--;

if(stack.get(top).next!=null)

{

stack.get(top).next.last=stack.get(top).last;

stack.get(top).last.next=stack.get(top).next;

}

else

{

stack.get(top).last.next=stack.get(top).next;

}

stack.remove(top);

}

public int top() {

return stack.get(top-1).x;

}

public int getMin() {

return Head.next.x;

}

}

class element{

int x;

element next;

element last;

element(int p){

x=p;

next=null;

last=null;

}

}

Submission Result: Time Limit Exceeded

这道题的关键之处就在于 minStack 的设计,push() pop() top() 这些操作Java内置的Stack都有,不必多说。

我最初想着再弄两个数组,分别记录每个元素的前一个比它大的和后一个比它小的,想复杂了。

第一次看上面的代码,还觉得它有问题,为啥只在 x<minStack.peek() 时压栈?如果,push(5), push(1), push(3) 这样minStack里不就只有5和1,这样pop()出1后, getMin() 不就得到5而不是3吗?其实这样想是错的,因为要想pop()出1之前,3就已经被pop()出了。.

minStack 记录的永远是当前所有元素中最小的,无论 minStack.peek() 在stack 中所处的位置。

class MinStack {

// stack: store the stack numbers

private Stack<Integer> stack = new Stack<Integer>();

// minStack: store the current min values

private Stack<Integer> minStack = new Stack<Integer>();

public void push(int x) {

// store current min value into minStack

if (minStack.isEmpty() || x <= minStack.peek())

minStack.push(x);

stack.push(x);

}

public void pop() {

// use equals to compare the value of two object, if equal, pop both of them

if (stack.peek().equals(minStack.peek()))

minStack.pop();

stack.pop();

}

public int top() {

return stack.peek();

}

public int getMin() {

return minStack.peek();

}

}

时间: 2024-10-29 12:29:04

Leetcode#155Min Stack的相关文章

【LeetCode-面试算法经典-Java实现】【155-Min Stack(最小栈)】

[155-Min Stack(最小栈)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. top() –

[LeetCode]题解(python):155-Min Stack

题目来源: https://leetcode.com/problems/min-stack/ 题意分析: 实现一个小的栈,包括初始化,push,pop,top,和getMin. 题目思路: 私用是用两个数组来处理. 代码(python): 1 class MinStack(object): 2 def __init__(self): 3 """ 4 initialize your data structure here. 5 """ 6 self

LeetCode 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. top() -- Get the top element. getMin() -- Retrieve the minimum e

leetcode Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

[LeetCode] 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. top() -- Get the top element. getMin() -- Retrieve the minimum e

LeetCode: Min Stack 解题报告

Min Stack My Submissions Question Solution 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. top() -- Get the top

LeetCode——Min Stack

Description: 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. top() -- Get the top element. getMin() -- Retrieve

[LeetCode] Min Stack Min Stack

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. top() -- Get the top element. getMin() -- Retrieve the

leetcode ----Trie/stack专题

一:Implement Trie (Prefix Tree) 题目: Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z. 分析:此题是典型的trie树.能够參见:http://blog.csdn.net/lu597203933/article/details/44227431