Python实现包含min函数的栈
本文实例讲述了Python实现包含min函数的栈。分享给大家供大家参考,具体如下:
# coding=utf8 ''' 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数。 在该栈中,调用min、push及pop的时间复杂度都是O(1)。 ''' class Stack(): def __init__(self): self.main_stack = [] # 辅助栈,每次次最小的元素压入辅助栈 self.assist_stack = [] # 记录栈中的最小元素 self._min = None def min(self): return self._min def push(self, data): self.main_stack.append(data) if self._min is None: self._min = data else: if data < self._min: self._min = data # 将最小的元素压入辅助栈 self.assist_stack.append(self._min) def pop(self): if len(self.main_stack) == 0: raise Exception('no data') elif len(self.main_stack) == 1: self.assist_stack.pop() self._min = None return self.main_stack.pop() else: self.assist_stack.pop() self._min = self.assist_stack[-1] return self.main_stack.pop() if __name__ == '__main__': s = Stack() s.push(3) s.push(4) s.push(2) s.push(1) print s.min() s.pop() s.pop() print s.min() s.pop() print s.min() s.pop() print s.min() s.pop()
更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

When using Scapy crawler, the reason why pipeline persistent storage files cannot be written? Discussion When learning to use Scapy crawler for data crawler, you often encounter a...

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

Python process pool handles concurrent TCP requests that cause client to get stuck. When using Python for network programming, it is crucial to efficiently handle concurrent TCP requests. ...

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

How to handle high resolution images in Python to find white areas? Processing a high-resolution picture of 9000x7000 pixels, how to accurately find two of the picture...

Data Conversion and Statistics: Efficient Processing of Large Data Sets This article will introduce in detail how to convert a data list containing product information to another containing...
