forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1826.java
46 lines (45 loc) · 1.36 KB
/
_1826.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
package com.fishercoder.solutions;
public class _1826 {
public static class Solution1 {
public int badSensor(int[] sensor1, int[] sensor2) {
//check if sensor2 is faulty
int i = 0;
int j = 0;
for (; i < sensor1.length && j < sensor2.length - 1; ) {
if (sensor1[i] != sensor2[j]) {
i++;
} else {
i++;
j++;
}
}
boolean sensor2Faulty = false;
if (j == sensor2.length - 1 && i == sensor1.length) {
sensor2Faulty = true;
}
//check sensor1
i = 0;
j = 0;
for (; i < sensor1.length - 1 && j < sensor2.length; ) {
if (sensor1[i] != sensor2[j]) {
j++;
} else {
i++;
j++;
}
}
boolean sensor1Faulty = false;
if (i == sensor1.length - 1 && j == sensor2.length) {
sensor1Faulty = true;
}
if (sensor1Faulty && sensor2Faulty) {
return -1;
} else if (sensor1Faulty) {
return 1;
} else if (sensor2Faulty) {
return 2;
}
return -1;
}
}
}