力扣打卡day03

数组–前缀和

1.303. 区域和检索 - 数组不可变

class NumArray {
    int preSum[];
    public NumArray(int[] nums) {
        preSum=new int[nums.length+1];
        preSum[0]=0;
        for(int i=1;i<preSum.length;i++){
            preSum[i]=preSum[i-1]+nums[i-1];
        }
    }
    
    public int sumRange(int left, int right) {
       int result=0;
       result=preSum[right+1]-preSum[left];
       return result;
    }
}

2.304. 二维区域和检索 - 矩阵不可变

class NumMatrix {
    int preSum[][];
    public NumMatrix(int[][] matrix) {
        int m=matrix.length;
        int n=matrix[0].length;
        preSum=new int[m+1][n+1];
        for(int i=1;i<m+1;i++){
            for(int j=1;j<n+1;j++){
                preSum[i][j]=preSum[i-1][j]+preSum[i][j-1]-preSum[i-1][j-1]+matrix[i-1][j-1];
                //matrix为啥是i-1和j-1不是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];
        //这个为啥都要加一

    }
}

数组–差分数组

3. 1109. 航班预订统计

class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n){
        int[] nums=new int[n];
        Difference diff=new Difference(nums);
        for(int[] booking:bookings){
            int i=booking[0]-1;
            int j=booking[1]-1;
            int val=booking[2];
            diff.increment(i,j,val);
        }
        return diff.result();
    }
}
class Difference{
    private int[] diff;
    public Difference(int[] nums){
        diff=new int[nums.length];
        diff[0]=nums[0];
        for(int i=1;i<nums.length;i++){
            diff[i]=nums[i]-nums[i-1];
        }
    }
    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;
    }
}

4.1094. 拼车

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] nums=new int[1001];
        Difference diff=new Difference(nums);
        for(int[] trip:trips){
            int val=trip[0];
            int i=trip[1];
            //trip[1] 站有trip[0]个乘客上车,trip[2] 站有trip[0]个乘客下车
            //即乘客在车上的区间是 [trip[1], trip[2] - 1]
            int j=trip[2]-1;
            diff.increment(i,j,val);
        }
        int[] res=diff.result();
        for(int i=0;i<res.length;i++){
            if(res[i]>capacity){
                return false;
            }
        }
        return true;
    }
}
class Difference{
    private int[] diff;
    public Difference(int[] nums){
        diff=new int[nums.length];
        diff[0]=nums[0];
        for(int i=1;i<nums.length;i++){
            diff[i]=nums[i]-nums[i-1];
        }
    }
    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;
    }
}

5.370区间加法

int[] getModifiedArray(int length, int[][] updates) {
	// nums 初始化为全 0
	int[] nums = new int[length];
	// 构造差分解法
	Difference df = new Difference(nums);
	for (int[] update : updates) {
		int i = update[0];
		int j = update[1];
		int val = update[2];
		df.increment(i, j, val);
	}
	return df.result();
}
class Difference{
    private int[] diff;
    public Difference(int[] nums){
        diff=new int[nums.length];
        diff[0]=nums[0];
        for(int i=1;i<nums.length;i++){
            diff[i]=nums[i]-nums[i-1];
        }
    }
    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]=res[i-1]+diff[i];
        }
        return res;
    }
}

6.59. 螺旋矩阵 II

class Solution {
    public int[][] generateMatrix(int n) {
        int count=1;//填充的数字
        int start=0;//每次循环的开始点
        int offset=1;//左闭右开不包含终止值,终止值每圈都变
        int loop=n/2;
        int[][] nums=new int[n][n];
        int i,j;
        while(loop-->0){
            for(j=start;j<n-offset;j++){
                nums[start][j]=count++;
            }
            for(i=start;i<n-offset;i++){
                nums[i][j]=count++;
            }
            for(;j>start;j--){
                nums[i][j]=count++;
            }
            for(;i>start;i--){
                nums[i][j]=count++;
            }
            start++;
            offset++;
        }
        if(n%2==1){
            nums[start][start]=count;
        }
        return nums;
    }
}

7.54 螺旋矩阵

List<Integer> spiralOrder(int[][] matrix) {
    int m = matrix.length, n = matrix[0].length;
    int upper_bound = 0, lower_bound = m - 1;
    int left_bound = 0, right_bound = n - 1;
    List<Integer> res = new LinkedList<>();
    // res.size() == m * n 则遍历完整个数组
    while (res.size() < m * n) {
        if (upper_bound <= lower_bound) {
            // 在顶部从左向右遍历
            for (int j = left_bound; j <= right_bound; j++) {
                res.add(matrix[upper_bound][j]);
            }
            // 上边界下移
            upper_bound++;
        }
        
        if (left_bound <= right_bound) {
            // 在右侧从上向下遍历
            for (int i = upper_bound; i <= lower_bound; i++) {
                res.add(matrix[i][right_bound]);
            }
            // 右边界左移
            right_bound--;
        }
        
        if (upper_bound <= lower_bound) {
            // 在底部从右向左遍历
            for (int j = right_bound; j >= left_bound; j--) {
                res.add(matrix[lower_bound][j]);
            }
            // 下边界上移
            lower_bound--;
        }
        
        if (left_bound <= right_bound) {
            // 在左侧从下向上遍历
            for (int i = lower_bound; i >= upper_bound; i--) {
                res.add(matrix[i][left_bound]);
            }
            // 左边界右移
            left_bound++;
        }
    }
    return res;
}

8.旋转图像

//将二维矩阵原地顺时针旋转 90 度
public void rotate(int[][] matrix) {
    int n = matrix.length;
    // 先沿对角线镜像对称二维矩阵
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            // swap(matrix[i][j], matrix[j][i]);
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }
    // 然后反转二维矩阵的每一行
    for (int[] row : matrix) {
        reverse(row);
    }
}

// 反转一维数组
void reverse(int[] arr) {
    int i = 0, j = arr.length - 1;
    while (j > i) {
        // swap(arr[i], arr[j]);
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        i++;
        j--;
    }
}

你可能感兴趣的:(leetcode,算法,排序算法)