package nowCoderClass1.section4;import java.util.Arrays;/*** 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。* Created by Dell on 2017年05月07日.*/public class Solution {int[] stack=new int[100];int[] stackMin=new int[100];int count=0;public void push(int node) {if(count==stack.length){expandCapacity();}stack[count]=node;//得到栈最小元素的if(count==0){stackMin[count]=node;}else{stackMin[count]=stack[count]<stackMin[count-1]?stack[count]:stackMin[count-1];}count++;}private void expandCapacity() {Arrays.copyOf(stack,stack.length*2);Arrays.copyOf(stackMin,stackMin.length*2);}/*** 弹出一个元素*/public void pop() {if(count>0){count--;stack[count]=0;stackMin[count]=0;}}public int top() {if(count==0)return -1;return stack[count-1];}public int min() {if(count==0)return -1;return stackMin[count-1];}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。