problem0225

package
v0.0.0-...-899dd15 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 0 Imported by: 0

README

225. 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:

  1. You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
  2. Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  3. You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

Credits:Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

解题思路

见程序注释

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MyStack

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

MyStack 是用 Queue 实现的 栈

func Constructor

func Constructor() MyStack

Constructor Initialize your data structure here.

func (*MyStack) Empty

func (ms *MyStack) Empty() bool

Empty Returns whether the stack is empty.

func (*MyStack) Pop

func (ms *MyStack) Pop() int

Pop Removes the element on top of the stack and returns that element.

func (*MyStack) Push

func (ms *MyStack) Push(x int)

Push Push element x onto stack.

func (*MyStack) Top

func (ms *MyStack) Top() int

Top Get the top element.

type Queue

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

Queue 是用于存放 int 的队列

func NewQueue

func NewQueue() *Queue

NewQueue 返回 *kit.Queue

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty 反馈 q 是否为空

func (*Queue) Len

func (q *Queue) Len() int

Len 返回 q 的长度

func (*Queue) Pop

func (q *Queue) Pop() int

Pop 从 q 中取出最先进入队列的值

func (*Queue) Push

func (q *Queue) Push(n int)

Push 把 n 放入队列

Jump to

Keyboard shortcuts

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