C# Solution (Max Consecutive Ones)


  • 0
    A
    public int FindMaxConsecutiveOnes(int[] nums) {
            int MaxOnesCoutner = 0;
                bool onlyOneOne = false;
                if (nums.Length == 1) return nums[0];
                for (int i = 0; i < nums.Length-1; i++)
                {
                    if(nums[i]!=0 && nums[i+1]==1)
                    {
                        nums[i + 1] = nums[i] + 1;
                        if (nums[i + 1] > MaxOnesCoutner)
                            MaxOnesCoutner = nums[i + 1];
                    }
                    if (nums[i] == 1)
                        onlyOneOne = true;
                }
                if (nums[nums.Length - 1] == 1 && MaxOnesCoutner < 2)
                    return 1;
                if (onlyOneOne == true & MaxOnesCoutner == 0) return 1;
                return MaxOnesCoutner;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.