LeetCode 2249. 统计圆内格点数目(枚举)

该博客介绍了LeetCode第2249题的解决方案,主要探讨了两种方法:超时的深度优先搜索(DFS)策略和枚举格点的方法。DFS策略通过枚举所有圆并使用DFS找出圆内的格点,但可能会导致超时。而枚举格点的方法则是遍历200x200范围内的所有点,检查它们是否位于任意圆内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

方法一:DFS(超时)

算法流程:

  • 枚举所有圆,对于每个圆,从圆心出发,利用DFS将圆内的格点都保存在集合中

  • 利用Set去除重复格点,最后返回Set大小即为出现在 至少一个 圆内的 格点数目

class Solution {
    public int countLatticePoints(int[][] circles) {
        int n = circles.length;

        List<Grid>[] grids = new List[n];
        // 1. 枚举圆,统计每个圆内的格点
        for (int i = 0; i < n; i++) {
            boolean[][] visited = new boolean[201][201];
            grids[i] = new ArrayList<>();
            dfs(circles[i][0], circles[i][1], circles[i][0], circles[i][1], circles[i][2], visited, grids[i]);
        }
        Set<Grid> set = new HashSet<>();
        // 2. Set 去重
        for(List<Grid> list : grids) {
            set.addAll(list);
        }
        return set.size();
    }

    private void dfs(int x, int y, int nx, int ny, int r, boolean[][] visited, List<Grid> list) {

        if (!isInCircle(x, y, r, nx, ny) || visited[nx][ny]) {
            return ;
        }
        visited[nx][ny] = true;
        list.add(new Grid(nx, ny));

        // 上 右 下 左
        dfs(x, y, nx - 1, ny, r, visited, list);
        dfs(x, y, nx, ny + 1, r, visited, list);
        dfs(x, y, nx + 1, ny, r, visited, list);
        dfs(x, y, nx, ny - 1, r, visited, list);
    }

    private boolean isInCircle(int x, int y, int r, int nx, int ny) {
        return Math.pow(nx - x, 2) + Math.pow(ny - y, 2) <= Math.pow(r, 2);
    }

    class Grid {
        int x;
        int y;
        public Grid(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Grid grid = (Grid) o;
            return x == grid.x &&
                    y == grid.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }
}

方法二:枚举格点

枚举 200 × 200 200 \times 200 200×200范围内的所有点,并判断是否在某一个圆内

class Solution {
    public int countLatticePoints(int[][] circles) {
        int cnt = 0;
        for (int nx = 0; nx <= 200; nx++) {
            for (int ny = 0; ny <= 200; ny++) {
                for (int[] c : circles) {
                    int x = c[0], y = c[1], r = c[2];
                    if (isInCircle(x, y, r, nx, ny)) {
                        cnt++;
                        break;
                    }
                }
            }
        }
        return cnt;
    }
    private boolean isInCircle(int x, int y, int r, int nx, int ny) {
        return Math.pow(nx - x, 2) + Math.pow(ny - y, 2) <= Math.pow(r, 2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xylitolz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值