PowerStack

int curInc;
HashMap<Integer, Integer> incMap;
Stack<Integer> stack;


public SuperStack() {
    this.curInc = 0;
    this.incMap = new HashMap<Integer, Integer>();
    this.stack = new Stack<Integer>();
}

        
private void push(int val) {
    this.stack.push(val);
}

        
private int pop() {
    if (incMap.containsKey(stack.size())) {
        curInc += incMap.get(stack.size());
    }
    int ret = stack.pop() + curInc;
    return ret;
}


private void inc(int index, int inc) {
    if (incMap.containsKey(index)) {
        incMap.put(index, incMap.get(index) + inc);
    } else {
        incMap.put(index, inc);
    }
}

 

 

Normal Solution

private void push(int val) {
    list.addLast(val);
}

private int pop() {
        return list.pollLast();
}

private void inc(int a, int b) {
    for (int i = 0; i < a; i++) {
        list.set(i, list.get(i) + b);
    }
}

 

posted @ 2015-10-25 02:23  airforce  阅读(336)  评论(0编辑  收藏  举报