题目链接https://leetcode.cn/problems/points-that-intersect-with-cars/description/?envType=daily-question&envId=2024-09-15
给你一个下标从 0 开始的二维整数数组 nums 表示汽车停放在数轴上的坐标。对于任意下标 i,nums[i] = [starti, endi] ,其中 starti 是第 i 辆车的起点,endi 是第 i 辆车的终点。
返回数轴上被车 任意部分 覆盖的整数点的数目。
示例 1:
输入:nums = [[3,6],[1,5],[4,7]] 输出:7 解释:从 1 到 7 的所有点都至少与一辆车相交,因此答案为 7 。
示例 2:
输入:nums = [[1,3],[5,8]] 输出:7 解释:1、2、3、5、6、7、8 共计 7 个点满足至少与一辆车相交,因此答案为 7 。
我的思路:
遍历,然后利用Set添加时去重,最后结果就是Set中元素的个数。
代码:
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
Set s = new HashSet();
for(List<Integer> m : nums){
int i = m.get(0);
int j = m.get(1);
for(;i<=j;i++){
s.add(i);
}
}
return s.size();
}
}
结果:

![[Python]一、Python基础编程](https://i-blog.csdnimg.cn/direct/f602a12f2f6a44c6b5953c23294b7084.png)

















