LeetCode复习 设计系列

#303 区域和检索 - 数组不可变

static class NumArray {
        int[] preSum;
        public NumArray(int[] nums) {
            preSum = new int[nums.length];
            preSum[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                preSum[i] = nums[i] + preSum[i-1];
            }
            System.out.println();
        }

        public int sumRange(int left, int right) {
           return left == 0 ? preSum[right] : preSum[right] - preSum[left-1];
        }
    }


#146 LRU 缓存机制

 static class LRUCache {
        Map<Integer, Node1> map;
        int size;
        Node1 first;
        Node1 last;
        int cap;
        public LRUCache(int capacity) {
            map = new HashMap<>();
            size = 0;
            first = new Node1();
            last = new Node1();
            first.next = last;
            last.prev = first;
            cap = capacity;
        }

        //新增
        public void addToHead(Node1 node1) {
            //旧头节点
            Node1 oldFirst = first.next;
            first.next = node1;
            node1.prev = first;
            node1.next = oldFirst;
            oldFirst.prev = node1;
            size++;
        }

        //更新
        public void moveToHead(Node1 node1) {
            deleteNode(node1);
            addToHead(node1);
        }

        public Node1 removeTail() {
            Node1 oldTail = last.prev;
            deleteNode(oldTail);
            return oldTail;
        }

        public Node1 deleteNode(Node1 node1) {
            Node1 prev = node1.prev;
            Node1 next = node1.next;
            prev.next = next;
            next.prev = prev;
            size--;
            return node1;
        }

        public int get(int key) {
            if (map.containsKey(key)) {
                Node1 node1 = map.get(key);
                moveToHead(node1);
                return node1.val;
            }
            return -1;
        }

        public void put(int key, int value) {
            //存在就更新
            if (map.containsKey(key)) {
                Node1 node1 = map.get(key);
                node1.val = value;
                moveToHead(node1);
            } else {
            //不存在就新增
                Node1 node1 = new Node1(key, value);
                map.put(key,node1);
                addToHead(node1);
            }
            if (size > cap) {
                Node1 oldTail = removeTail();
                map.remove(oldTail.key);
            }
        }
    }


#304 二维区域和检索 - 矩阵不可变

class NumMatrix {
        int[][] preSum;
        public NumMatrix(int[][] matrix) {
            int r = matrix.length;
            int c = matrix[0].length;
            preSum = new int[r + 1][c + 1];
            for (int i = 0; i < r; i++) {
                for (int j = 0; j < c; j++) {
                    preSum[i+1][j+1] = preSum[i][j+1] + preSum[i+1][j] - preSum[i][j] + matrix[i][j];
                }
            }
        }

        public int sumRegion(int row1, int col1, int row2, int col2) {
            return preSum[row2+1][col2+1] - preSum[row2+1][col1] - preSum[row1][col2+1] + preSum[row1][col1];
        }
    }


#剑指 Offer 59 - II 队列的最大值

class MaxQueue {

        Deque<Integer> queue;
        Deque<Integer> maxQueue;
        public MaxQueue() {
            queue = new LinkedList<>();
            maxQueue = new LinkedList<>();
        }

        public int max_value() {
            return maxQueue.isEmpty() ? -1 : maxQueue.peek();

        }

        public void push_back(int value) {
            queue.offer(value);
            while (!maxQueue.isEmpty() && value > maxQueue.getLast()) {
                maxQueue.removeLast();
            }
            maxQueue.offer(value);
        }

        public int pop_front() {
              if (!queue.isEmpty() && Objects.equals(queue.peek(), maxQueue.peek())) {
                  queue.poll();
                return maxQueue.poll();
            }
            return queue.isEmpty() ? -1 : queue.poll();
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值