public int numComponents(ListNode head, int[] nums) {
Set<Integer> set=new HashSet<>();
for(int i=0;i<nums.length;i++){
set.add(nums[i]);
}
int ans=0;
while(head!=null){
if(set.contains(head.val)){
while(head!=null){
if(!set.contains(head.val))
break;
head=head.next;
}
ans++;
}else
head=head.next;
}
return ans;
}
2022.10.12-----leetcode.817
最新推荐文章于 2024-07-27 17:56:15 发布