二、矩阵区域和(Biweekly17)

题目描述:
给你一个 m * n 的矩阵 mat 和一个整数 K ,请你返回一个矩阵 answer ,其中每个 answer[i][j] 是所有满足下述条件的元素 mat[r][c] 的和:

i - K <= r <= i + K, j - K <= c <= j + K
(r, c) 在矩阵内。

示例 1:

输入:mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1
输出:[[12,21,16],[27,45,33],[24,39,28]]
示例 2:

输入:mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2
输出:[[45,45,45],[45,45,45],[45,45,45]]

提示:

m == mat.length
n == mat[i].length
1 <= m, n, K <= 100
1 <= mat[i][j] <= 100

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/matrix-block-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

使用硬方法

class Solution {
   public int[][] matrixBlockSum(int[][] mat, int K) {
        int m = mat.length;
        int n = mat[0].length;

        int [][]result = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                result[i][j] = get(mat,i,j,K);
            }
        }
                return result;

    }
    public int get(int [][]mat,int x,int y,int k){
//        四个方向
        int result = 0;
        int left = y - k >= 0 ? y - k : 0;
        int up = x - k >= 0 ? x - k : 0;
        int right = y + k < mat[0].length ? y + k : mat[0].length - 1;
        int down = x + k < mat.length ? x + k : mat.length - 1;
        for (int i = up; i <= down; i++) {
            for (int j = left; j <= right; j++) {
                result += mat[i][j];
            }
        }
        
        return result;


    }
}

看看别人实现的方法
使用前缀和

 public int[][] matrixBlockSum(int[][] mat, int K) {
        int row = mat.length;
        int col = mat[0].length;
        int[][] res = new int[row][col];
        int[][] dp = new int[row + 1][col + 1];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] - dp[i][j] + mat[i][j];
            }
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                // 左上角坐标
                int r1 = Math.max(i - K, 0);
                int c1 = Math.max(j - K, 0);
                // 右下角坐标
                int r2 = Math.min(i + K, row - 1);
                int c2 = Math.min(j + K, col - 1);
                res[i][j] = dp[r2 + 1][c2 + 1] - dp[r1][c2 + 1] - dp[r2 + 1][c1] + dp[r1][c1];
            }
        }
        return res;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中订阅ICS(iCalendar)日程,你可以使用一些开源的Java库,例如 iCal4j 或 Biweekly。这些库提供了解析和处理ICS文件的功能。 以下是使用 iCal4j 库来订阅ICS日程的简单示例: 1. 首先,你需要将 iCal4j 添加到你的项目依赖中。你可以在 Maven 或 Gradle 中添加以下依赖项: Maven: ```xml <dependency> <groupId>org.mnode.ical4j</groupId> <artifactId>ical4j</artifactId> <version>3.0.27</version> </dependency> ``` Gradle: ```groovy implementation 'org.mnode.ical4j:ical4j:3.0.27' ``` 2. 然后,你可以使用以下代码订阅ICS日程: ```java import net.fortuna.ical4j.data.CalendarBuilder; import net.fortuna.ical4j.model.Calendar; import net.fortuna.ical4j.model.Period; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; public class ICSReader { public static void main(String[] args) { try { // 从文件中读取ICS日程 FileInputStream fileInputStream = new FileInputStream("path/to/your/file.ics"); CalendarBuilder builder = new CalendarBuilder(); Calendar calendar = builder.build(fileInputStream); // 或者从URL中读取ICS日程 URL url = new URL("http://example.com/your-calendar.ics"); Calendar calendar = builder.build(url.openStream()); // 解析并输出日程信息 for (Object component : calendar.getComponents()) { if (component instanceof Period) { Period period = (Period) component; System.out.println("Start: " + period.getStart()); System.out.println("End: " + period.getEnd()); // 其他属性的访问方式请参考 iCal4j 文档 } } } catch (IOException e) { e.printStackTrace(); } catch (net.fortuna.ical4j.data.ParserException e) { e.printStackTrace(); } } } ``` 请注意,以上示例仅演示了如何使用 iCal4j 库来读取并解析ICS文件中的日程信息。你可以根据需要进一步处理和使用这些信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值