题解 | #设计getMin功能的栈#
设计getMin功能的栈
http://www.nowcoder.com/practice/05e57ce2cd8e4a1eae8c3b0a7e9886be
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); Deque<Integer> data=new LinkedList<>(); for(int i=0;i<=n;i++){ String line=sc.nextLine(); String[] strs=line.split(" "); String op=strs[0]; if("push".equals(op)){ data.addLast(Integer.parseInt(strs[1])); }else if("pop".equals(op)){ data.removeLast(); }else if("getMin".equals(op)){ Integer[] ints=data.toArray(new Integer[0]); Arrays.sort(ints); System.out.println(ints[0]); } } } }