标签:interview
Use another stack maintaining min.
push(T t) { mainStack.push(t); T curMin = minStack.peek(); if (curMin == null || t < curMin) { minStack.push(t); } } T peek() { return mainStack.peek(); } T poll() { T t = mainStack.poll(); if (t == null) return t; if (t == minStack.peek()) minStack.poll(); return t; } T min() { return minStack(); } Stack<T> mainStack; Stack<T> minStack;
标签:interview
原文地址:http://7371901.blog.51cto.com/7361901/1581746