6041. 多个数组求交集
题目描述
哈希
class Solution {
public List<Integer> intersection(int[][] nums) {
int n = nums.length;
int[] freq = new int[1001];
List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < nums[i].length; j++) {
freq[nums[i][j]]++;
}
}
for (int i = 1; i <= 1000; i++) {
if (freq[i] >= n) {
res.add(i);
}
}
return res;
}
}
6042. 统计圆内格点数目
题目描述
方法一:枚举圆
方法二:枚举格点
枚举 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);
}
}