485. Max Consecutive Ones

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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容