剑指offer第20题(包含min函数的栈)
来源:互联网 发布:淘宝店铺鞋子logo 编辑:程序博客网 时间:2024/06/05 04:20
题目:定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
java代码:
import java.util.Stack;import java.util.Iterator;public class Solution { Stack<Integer> stack1=new Stack<Integer>(); public void push(int node) { stack1.push(node); } public void pop() { stack1.pop(); } public int top() { return stack1.peek(); } public int min() { int min=stack1.peek(); int temp; Iterator<Integer> it=stack1.iterator(); while(it.hasNext()){ temp=it.next(); if(min>temp){ min=temp; } } return min; }}python代码;
# -*- coding:utf-8 -*-class Solution: stack=[] def push(self, node): self.stack.append(node) def pop(self): self.stack.pop() def top(self): return self.stack[0] def min(self): return min(self.stack)
阅读全文
0 0
- 包含min函数的栈 (剑指Offer 第 19 题)
- 剑指offer第20题(包含min函数的栈)
- 【剑指offer】包含min函数的栈
- 剑指offer--包含min函数的栈
- 剑指Offer-->包含min函数的栈
- 《剑指offer》包含min函数的栈
- 剑指offer - 包含min函数的栈
- 剑指offer-包含min函数的栈
- 剑指offer:包含min函数的栈
- 包含min函数的栈|剑指offer
- 剑指offer-包含min函数的栈
- 剑指offer 包含min函数的栈
- 剑指offer:包含min函数的栈
- 剑指offer 包含min函数的栈
- [剑指offer]包含min函数的栈
- 《剑指offer》-包含min函数的栈
- 剑指offer----包含min函数的栈
- 【剑指offer】包含min函数的栈
- LoadRunner测试多个用户账号是否可用
- 精通python网络爬虫笔记一
- 客户端app的demo的实现
- 【腾讯TMQ】像google一样测试系列之一:目录篇
- DOM对象和Jquery对象
- 剑指offer第20题(包含min函数的栈)
- jsp走向没落?
- JSON和XML的比较
- bzoj4636 蒟蒻的数列(离散化+线段树)
- c语言 字符串大小排序
- ucosii 多任务
- Hadoop Yarn Capacity调度器之ACL的一个坑
- B
- 关于echart在隐藏与显示轮替中的div加载显示为空的原因分析