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.
Already Pass Solution
version 1
public int FindMaxConsecutiveOnes(int[] nums)
{
// join
string[] temp = String.Join("",nums).Split('0');
int result = 0;
foreach (string i in temp)
{
result = result > i.Length ? result : i.Length;
}
return result;
}
version 2
public int FindMaxConsecutiveOnes(int[] nums) {
int result = 0;
int curMax = 0;
foreach(int i in nums)
{
if(i == 1)
{
curMax++;
result = result > curMax ? result : curMax;
}
else
{
curMax = 0;
}
}
return result;
解题思路:
1.使用Join和Split方法从int[]转到string[]
2.判断字符串数组元素的长度
有待思考:
1.不转换来进行求值(V2)