-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1493.java
More file actions
37 lines (34 loc) · 1.08 KB
/
LeetCode1493.java
File metadata and controls
37 lines (34 loc) · 1.08 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
public class LeetCode1493 {
public static void main(String[] args) {
// 输入:nums = [1,1,0,1]
// 输出:3
System.out.println(new Solution1493().longestSubarray(new int[] { 1, 1, 0, 1 }));
// 输入:nums = [0,1,1,1,0,1,1,0,1]
// 输出:5
System.out.println(new Solution1493().longestSubarray(new int[] { 0, 1, 1, 1, 0, 1, 1, 0, 1 }));
// 输入:nums = [1,1,1]
// 输出:2
System.out.println(new Solution1493().longestSubarray(new int[] { 1, 1, 1 }));
}
}
class Solution1493 {
public int longestSubarray(int[] nums) {
int ans = 0;
int start = 0;
boolean meet = false;
for (int end = 0; end < nums.length; end++) {
if (nums[end] == 0) {
if (meet) {
while (nums[start] != 0) {
start++;
}
start++;
} else {
meet = true;
}
}
ans = Math.max(ans, end - start);
}
return ans;
}
}