Solution

package
v0.0.0-...-48c9e09 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2019 License: MIT Imports: 1 Imported by: 0

README

155 Min Stack

Problem

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 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.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

Solution

Approach1
Approach2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MinStack

type MinStack struct {
	// contains filtered or unexported fields
}

func Constructor

func Constructor() MinStack

* initialize your data structure here.

func (*MinStack) GetMin

func (this *MinStack) GetMin() interface{}

func (*MinStack) Pop

func (this *MinStack) Pop()

func (*MinStack) Push

func (this *MinStack) Push(x interface{})

func (*MinStack) Top

func (this *MinStack) Top() interface{}

type Stack

type Stack []interface{}

1.将栈定义为一个借口方便插入任意类型数据 2.采用切片可以动态分配大小。还有Len() 和Cap()这个2个战术可以免费使用

func (Stack) Cap

func (stack Stack) Cap() int

返回栈的容量

func (Stack) IsEmpty

func (stack Stack) IsEmpty() bool

判断栈是否为空

func (Stack) Len

func (stack Stack) Len() int

返回栈现在元素个数

func (*Stack) Pop

func (stack *Stack) Pop() (interface{}, error)

元素出栈,如果栈为空则元素为空error不为空

func (*Stack) Push

func (stack *Stack) Push(value interface{})

元素入栈

func (Stack) Top

func (stack Stack) Top() (interface{}, error)

返回栈顶元素,如果栈为空则元素为空error不为空

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL