首页 > 其他 > 详细

PowerStack

时间:2015-10-25 06:07:25      阅读:288      评论:0      收藏:0      [点我收藏+]
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);
    }
}

 

PowerStack

原文:http://www.cnblogs.com/airwindow/p/4908159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!