-
Notifications
You must be signed in to change notification settings - Fork 0
/
817.链表组件.java
47 lines (43 loc) · 1.05 KB
/
817.链表组件.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/*
* @lc app=leetcode.cn id=817 lang=java
*
* [817] 链表组件
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public int numComponents(ListNode head, int[] nums) {
Set<Integer> set = new HashSet<>(Arrays.stream(nums).boxed().collect(Collectors.toList()));
int res = 0;
ListNode p = head;
boolean start = false;
while (p != null) {
if (set.contains(p.val)) {
if (start == false) {
start = true;
res++;
}
} else {
start = false;
}
p = p.next;
}
return res;
}
}
// @lc code=end