博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指offer系列-T21包含min函数的栈
阅读量:4285 次
发布时间:2019-05-27

本文共 1010 字,大约阅读时间需要 3 分钟。

        本人对java语言更熟悉,所以剑指offer代码都是通过Java实现,且涉及的核心代码全部通过牛客网的测试用例检查,感谢牛客网为我检验程序提供了极大帮助!main函数是为了在自己运行程序时,运行结果更直观化。

/** * @author xhl * 包含min函数的栈 * 题目描述 * 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素。  * 要求函数min、push 以及pop 的时间复杂度都是O(1)。 * 思路:在定义的栈中,再加入辅助栈,用于记录每次栈元素变化时的最小值变化, * 每push进一个元素,辅助栈也push进新元素加入后的最小值(可能还是原最小值,所以会重复) * 每pop出一个元素,辅助栈也pop出一个元素,反应减少该元素后,栈的最小值。 */public class offerT21 {	/**	 * @param args	 */	public static void main(String[] args) {		// TODO Auto-generated method stub		stack sta=new stack();		sta.push(1);		sta.push(9);		sta.push(0);		sta.push(3);		System.out.println(sta.min());		sta.pop();		System.out.println(sta.min());		sta.pop();		System.out.println(sta.min());			}}/*定义的栈结构*/class stack{	Stack
stack=new Stack
(); Stack
MinNumberstack=new Stack
(); public void push(int node) { stack.push(node); if(MinNumberstack.isEmpty())//空时直接push MinNumberstack.push(node); else{//非空时,先判断出当前最小值,再push进最小值 if(MinNumberstack.peek()

转载地址:http://jipgi.baihongyu.com/

你可能感兴趣的文章
java 之注解
查看>>
java之struts(二)国际化、拦截器
查看>>
iOS之真机和模拟器的CPU架构器架构\Xcode中和symbols有关的几个设置
查看>>
java之maven的使用/eclipse中maven项目部署到tomcat的几种方法
查看>>
iOS之常用分类frame、button、
查看>>
微信小程序常用快捷键
查看>>
小程序中相关控件的使用、样式的使用、flex布局
查看>>
开篇词
查看>>
论文笔记:Event Detection with Trigger-Aware Lattice Neural Network
查看>>
《Recurrent Chunking Mechanisms for Long-Text Machine Reading Comprehension》--论文分享
查看>>
论文笔记丨Multi-Level Matching and Aggregation Network for Few-Shot Relation Classification
查看>>
论文笔记 | All NLP Tasks Are Generation Tasks: A General Pretraining Framework
查看>>
论文笔记 | Towards Interpreting BERT for Reading Comprehension Based QA
查看>>
论文笔记 | Transformer-XL:Attentive Language Models Beyond a Fixed-Length Context
查看>>
论文笔记 | An End-to-End Deep Framework for Answer Triggering with a Novel Group-Level Objective
查看>>
论文笔记:Exploiting WordNet Synset and Hypernym Representations for Answer Selection
查看>>
论文笔记|Overcoming the challenge for text classification in the open world
查看>>
论文笔记 | FLAT: Chinese NER Using Flat-Lattice Transformer
查看>>
论文笔记 | Multi-Grained Named Entity Recognition
查看>>
论文解读 | 百度 ERNIE: Enhanced Representation through Knowledge Integration
查看>>