-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path817.java
More file actions
61 lines (60 loc) · 1.62 KB
/
817.java
File metadata and controls
61 lines (60 loc) · 1.62 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
__________________________________________________________________________________________________
sample 1 ms submission
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int numComponents(ListNode head, int[] G) {
boolean [] vis=new boolean[10000];
for(int i:G){
vis[i]=true;
}
int number=0;
while(head!=null){
if(vis[head.val]&&((head.next==null)||!vis[head.next.val])){
number++;
}
head=head.next;
}
return number;
}
}
__________________________________________________________________________________________________
sample 38888 kb submission
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int numComponents(ListNode head, int[] G) {
ListNode tmp = head;
List<Integer> lst = Arrays.stream(G).boxed().collect(Collectors.toList());
boolean found = false;
int res = 0;
while(tmp != null) {
if(lst.contains(tmp.val)){
found = true;
}
else {
if(found){
res++;
found = false;
}
}
tmp = tmp.next;
}
if(found)
res++;
return res;
}
}
__________________________________________________________________________________________________