码迷,mamicode.com
首页 > 其他好文 > 详细

PowerStack

时间:2015-10-25 06:07:34      阅读:148      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!