zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    记账本开发第一天-补
    20200418-补
    20200411-补
    20200404-补
    20200328-补
    暴力解N皇后
    nN皇后递归
    Hanoi汉诺塔非递归栈解
    Hanoi汉诺塔递归
    JMMjmm模型
  • 原文地址:https://www.cnblogs.com/airwindow/p/4908159.html
Copyright © 2011-2022 走看看