最小栈
题目
lettcode-155题
难度:简单
解题思路
题目基本上就是要求找出一个栈中最小的值,通过java中内置的栈的方法即可实现这一目标
首先我的想法是通过遍历比较所有的值来找出最小的那个值,但是这样子消耗了较多的时间,而且题目希望是设计一种栈的结构,而不是直接用栈的方法。
所以后面换了一种新的方法,就是使用链表来实现,这样子的效率是较高的,相比于之前的方法,用时整整少了200多毫秒
具体实现方式如下
具体代码(第一种思路)
1 | class MinStack { |
具体代码(第二种思路)
1 | class MinStack { |