C#LeetCode刷题之#485-最大连续1的个数（Max Consecutive Ones）

Given a binary array, find the maximum number of consecutive 1s in this array.

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.The maximum number of consecutive 1s is 3.

Note:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000

```public class Program {

public static void Main(string[] args) {
int[] nums = null;

nums = new int[] { 1, 1, 0, 1, 1, 1 };
var res = FindMaxConsecutiveOnes(nums);
Console.WriteLine(res);

res = FindMaxConsecutiveOnes2(nums);
Console.WriteLine(res);

}

private static int FindMaxConsecutiveOnes(int[] nums) {
int count = 0;
int max = 0;
for(int i = 0; i < nums.Length; i++) {
if(nums[i] == 0) {
if(count > max) max = count;
count = 0;
} else {
count++;
}
}
return count > max ? count : max;
}

private static int FindMaxConsecutiveOnes2(int[] nums) {
//同FindMaxConsecutiveOnes，只是写法稍微好看一点
int max = 0;
for(int i = 0, count = 0; i < nums.Length; i++) {
count = nums[i] == 1 ? count + 1 : 0;
max = count > max ? count : max;
}
return max;
}

}```

```3
3```

(1)