标签:interview
interface SetOfStacks<T> { push(T t); T pop(); T popAt(); static final int SUM_LENGTH = 10; } class SetOfStacksImpl<T> implements SetOfStacks { Stack<T>[] subStacks; int cur; SetOfStacksImpl() { // Initialize sub stacks array. subStacks = new Stack<T>[100]; subStacks[0] = initStack(); cur = 0; } push(T t) { if (t == null) return; if (subStacks[cur].isFull()) { if (cur >= subStacks.length) { // resize subStacks // or throw new OverFlowException(); } // Add new stack at head. Stack<T> newStack = initStack(); cur++; } subStacks[cur].push(t); } T pop() { T t = subStacks[cur].pop; while (curStacks[cur].isEmpty() && cur >= 0) { cur --; } return t; } T popAt(int i) { if (i > cur || i >= subStacks.length || i < 0) return null; T t = subStacks[i].pop(); // Only shrink when i is cur if (i == cur) { while (curStacks[cur].isEmpty() && cur >= 0) cur --; } return t; } }
A better solution? Instead of using a linked list holding sub stacks, using a map<stackIndex, stack>.
The benefit is that popAt(int) would be O(1).
Not a huge difference.
标签:interview
原文地址:http://7371901.blog.51cto.com/7361901/1582220