mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
21 lines
456 B
C#
21 lines
456 B
C#
|
public class Solution {
|
||
|
public int BitToInt(int num) => num == 0 ? 1 : 0;
|
||
|
|
||
|
public int LongestSubarray(int[] nums) {
|
||
|
var longest = 0;
|
||
|
|
||
|
var zeros = 0;
|
||
|
for (int l = 0, r = 0; r < nums.Length; ++r) {
|
||
|
zeros += BitToInt(nums[r]);
|
||
|
|
||
|
for (; zeros > 1; ++l) {
|
||
|
zeros -= BitToInt(nums[l]);
|
||
|
}
|
||
|
|
||
|
longest = Math.Max(longest, r - l);
|
||
|
}
|
||
|
|
||
|
return longest;
|
||
|
}
|
||
|
}
|