元月's blog 元月's blog
首页
  • 基础
  • 并发编程
  • JVM
  • Spring
  • Redis篇
  • Nginx篇
  • Kafka篇
  • Otter篇
  • Shardingsphere篇
  • 设计模式
  • MySQL
  • Oracle
  • 基础
  • 操作系统
  • 网络
  • 数据结构
  • 技术文档
  • Git常用命令
  • GitHub技巧
  • 博客搭建
  • 开发工具
更多

元月

临渊羡鱼,不如退而结网
首页
  • 基础
  • 并发编程
  • JVM
  • Spring
  • Redis篇
  • Nginx篇
  • Kafka篇
  • Otter篇
  • Shardingsphere篇
  • 设计模式
  • MySQL
  • Oracle
  • 基础
  • 操作系统
  • 网络
  • 数据结构
  • 技术文档
  • Git常用命令
  • GitHub技巧
  • 博客搭建
  • 开发工具
更多
  • 基础

  • 数据结构

    • 链表的基本操作:查找、删除
    • 链表的高级操作:链表的反转、合并、拆分
    • 链表的特殊状态:相交链表、回文链表、环形链表
    • 数组篇:双指针技巧
    • 数组篇:前缀和数组和差分数组
      • 一、前缀和数组
        • 303. 区域和检索 - 数组不可变
        • 剑指 Offer II 013. 二维子矩阵的和
      • 二、差分数组
        • 1094. 拼车
        • 1109. 航班预订统计
    • 数据结构与算法--跳表
    • 数据结构与算法--LRU算法
    • 数据结构与算法--LFU算法
  • 网络

  • 操作系统

  • 计算机网络
  • 数据结构
元月
2022-12-03
目录

数组篇:前缀和数组和差分数组

# 一、前缀和数组

# 303. 区域和检索 - 数组不可变 (opens new window)

☀️☀️☀️☀️☀️☀️题解一:一维前缀和数组

class NumArray {

    //前缀和
    int[] preSum;
    public NumArray(int[] nums) {
        int m = nums.length;
        preSum = new int[m+1];
        for(int i=0;i<m;i++){
            preSum[i+1] = preSum[i]+nums[i];
        }
    }
    
    public int sumRange(int left, int right) {
        return preSum[right+1] - preSum[left];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 剑指 Offer II 013. 二维子矩阵的和 (opens new window)

☀️☀️☀️☀️☀️☀️题解一:二维前缀和数组

class NumMatrix {
		//前缀和
    private int[][] preSum;

    public NumMatrix(int[][] matrix) {
        int m = matrix.length,n = matrix[0].length;
        preSum = new int[m+1][n+1];
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                preSum[i+1][j+1] = preSum[i][j+1] + preSum[i+1][j] + matrix[i][j] - preSum[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];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 二、差分数组

差分数组工具类

//差分数组工具类
    class Difference {
        //差分数组
        private int[] diff;

        public Difference(int[] nums) {
            //初始化差分数组 diff[i] = nums[i] - nums[i - 1];
            diff = new int[nums.length];
            diff[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                diff[i] = nums[i] - nums[i - 1];
            }
        }

        //给区间[i,j]增加val
        public void increment(int i, int j, int val) {
            diff[i] += val;
            if (j + 1 < diff.length) {
                diff[j + 1] -= val;
            }
        }

        //返回结果
        public int[] result() {
            int[] res = new int[diff.length];
            res[0] = diff[0];
            for (int i = 1; i < diff.length; i++) {
                res[i] = diff[i] + res[i - 1];
            }
            return res;
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 1094. 拼车 (opens new window)

☀️☀️☀️☀️☀️☀️题解一:

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] nums = new int[1001];
        Difference difference = new Difference(nums);
        for (int[] trip : trips) {
            // 即乘客在车上的区间是 [trip[1], trip[2] - 1]
            difference.increment(trip[1], trip[2] - 1, trip[0]);
        }

        int[] res = difference.result();
        // 客车自始至终都不应该超载
        for (int i = 0; i < res.length; i++) {
            if(res[i] > capacity){
                return false;
            }
        }
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 1109. 航班预订统计 (opens new window)

☀️☀️☀️☀️☀️☀️题解一:

class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] res = new int[n];
        Difference difference = new Difference(res);
        for(int[] tmp : bookings){
            difference.increment(tmp[0]-1,tmp[1]-1,tmp[2]);
        }
        return difference.result();
    }
}
1
2
3
4
5
6
7
8
9
10
数组篇:双指针技巧
数据结构与算法--跳表

← 数组篇:双指针技巧 数据结构与算法--跳表→

最近更新
01
otter二次开发-支持按目标端主键索引Load数据
08-03
02
mvnw简介
06-21
03
gor流量复制工具
06-03
更多文章>
Theme by Vdoing | Copyright © 2022-2024 元月 | 粤ICP备2022071877号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式