JAVA刷题记录: 专题四 前缀和

【模板】前缀和_牛客题霸_牛客网

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), q = in.nextInt();
        long[] arr = new long[n + 1];
        long[] dp = new long[n + 1];
        for(int i = 1; i <= n; i++) arr[i] = in.nextInt();
        dp[0] = 0; dp[1] = arr[1];
        for(int i = 2; i <= n; i++) dp[i] += dp[i - 1] + arr[i];
        for(int i = 0; i < q; i++) {
            int left = in.nextInt(), right = in.nextInt();
            System.out.println(dp[right] - dp[left - 1]);
        }
    }
}

【模板】二维前缀和_牛客题霸_牛客网

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
        int[][] arr = new int[n + 1][m + 1];
        for(int i = 1; i <= n; i++) 
            for(int j = 1; j <= m; j++) arr[i][j] = in.nextInt();
        long[][] dp = new long[n + 1][m + 1];
        for(int i = 1; i <= n; i++) 
            for(int j = 1; j <= m; j++)
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + arr[i][j] - dp[i - 1][j - 1];
        while(q > 0) {
            int x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt();
            System.out.println(dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1]);
            q--;
        }
    }
}

724. 寻找数组的中心下标 - 力扣(LeetCode)

class Solution {
    public int pivotIndex(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        for(int i = 1; i < n; i++) {
            f[i] = f[i - 1] + nums[i - 1];
        }
        for(int i = n - 2; i >= 0; i--) {
            g[i] = g[i + 1] + nums[i + 1];
        }
        for(int i = 0; i < n; i++) {
            if(f[i] == g[i]) return i;
        }
        return -1;
    }
}

238. 除自身以外数组的乘积 - 力扣(LeetCode)

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] ret = new int[n];
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = 1; g[n - 1] = 1;
        for(int i = 1; i < n; i++) {
            f[i] = f[i - 1] * nums[i - 1];
        }
        for(int i = n - 2; i >= 0; i--) {
            g[i] = g[i + 1] * nums[i + 1];
        }
        for(int i = 0; i < n; i++) {
            ret[i] = f[i] * g[i];
        }
        return ret;
    }
}

560. 和为 K 的子数组 - 力扣(LeetCode)

class Solution {
    public int subarraySum(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        int sum = 0, ret = 0;
        hash.put(0,1);
        for(int x : nums) {
            sum += x;
            ret += hash.getOrDefault(sum - k, 0);
            hash.put(sum, hash.getOrDefault(sum, 0) + 1);
        }
        return ret;
    }
}

974. 和可被 K 整除的子数组 - 力扣(LeetCode)

class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        int sum = 0;
        hash.put(0, 1);
        int ret = 0;
        for(int x : nums) {
            sum += x;
            int target = (sum % k + k) % k;
            ret += hash.getOrDefault(target, 0);
            hash.put(target, hash.getOrDefault(target, 0) + 1);
        }
        return ret;
    }
}

525. 连续数组 - 力扣(LeetCode)

class Solution {
    public int findMaxLength(int[] nums) {
        Map<Integer, Integer> hash = new HashMap<>();
        int ret = 0, sum = 0;
        hash.put(0, -1);
        for(int i = 0; i < nums.length; i++) {
            sum += nums[i] == 0 ? -1 : 1;
            if(hash.containsKey(sum)) ret = Math.max(ret, i - hash.get(sum));
            else hash.put(sum, i); //只保留第一次的, 不存在才要更新 
        }
        return ret;
    }
}

1314. 矩阵区域和 - 力扣(LeetCode)

class Solution {
    public int[][] matrixBlockSum(int[][] mat, int k) {
        int n = mat.length, m = mat[0].length;
        int[][] dp = new int[n + 1][m + 1];
        for(int i = 1; i <= n; i++) 
            for(int j = 1; j <= m; j++) 
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1];
        int[][] ret = new int[n][m];
        for(int i = 0; i < n; i++) 
            for(int j = 0; j < m; j++) {
                int x1 = Math.max(0, i - k) + 1, x2 = Math.min(n - 1, i + k) + 1;
                int y1 = Math.max(0, j - k) + 1, y2 = Math.min(m - 1, j + k) + 1;
                ret[i][j] = dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1];
            }
        return ret;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值