-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (31 loc) · 864 Bytes
/
Solution.java
File metadata and controls
40 lines (31 loc) · 864 Bytes
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
package leetcode.longestSubarrayof1AfterDeletingOneElement;
class Solution {
public int longestSubarray(int[] nums) {
int numberOfZero = 0;
int j = 0;
int l = 0;
int max = 0;
for(int i = 0; i < nums.length; i++){
int number = nums[i];
if(number == 1){
j++;
} else if (number == 0) {
numberOfZero++;
}
while(numberOfZero > 1){
if(nums[l] == 0){
numberOfZero--;
}
l++;
}
if(max < (i - l)){
max = i - l;
}
}
return max;
}
public static void main(String[] args) {
Solution solution = new Solution();
solution.longestSubarray(new int[]{0,1,1,1,0,1,1,0,1});
}
}