485. Max Consecutive Ones

LeetCode easy original: C# #array #csharp #easy #leetcode #math #search
O texto da tarefa é traduzido do russo para o idioma selecionado. O código permanece sem alterações.

Дан бинарный array nums, return максимальное количество последовательных единиц в arrayе.

Exemplo:

Input: nums = [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.

C# solução

correspondente/original
public class Solution {
    public int FindMaxConsecutiveOnes(int[] nums) {
        int count = 0;
        int maxCount = 0;
        foreach (int num in nums) {
            if (num == 1) {
                count += 1;
            } else {
                maxCount = Math.Max(maxCount, count);
                count = 0;
            }
        }
        return Math.Max(maxCount, count);
    }
}

C++ solução

rascunho automático, revisar antes de enviar
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int FindMaxConsecutiveOnes(vector<int>& nums) {
        int count = 0;
        int maxCount = 0;
        foreach (int num in nums) {
            if (num == 1) {
                count += 1;
            } else {
                maxCount = max(maxCount, count);
                count = 0;
            }
        }
        return max(maxCount, count);
    }
}

Java solução

correspondente/original
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int count = 0;
        int maxCount = 0;
        for (int num : nums) {
            if (num == 1) {
                count += 1;
            } else {
                maxCount = Math.max(maxCount, count);
                count = 0;
            }
        }
        return Math.max(maxCount, count);
    }
}

JavaScript solução

correspondente/original
var findMaxConsecutiveOnes = function(nums) {
    let count = 0
    let maxCount = 0
    for (let num of nums) {
        if (num === 1) {
            count += 1
        } else {
            maxCount = Math.max(maxCount, count)
            count = 0
        }
    }
    return Math.max(maxCount, count)
}

Python solução

correspondente/original
class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        count = 0
        maxCount = 0
        for num in nums:
            if num == 1:
                count += 1
            else:
                maxCount = max(maxCount, count)
                count = 0
        return max(maxCount, count)

Go solução

correspondente/original
func findMaxConsecutiveOnes(nums []int) int {
    count := 0
    maxCount := 0
    for _, num := range nums {
        if num == 1 {
            count++
        } else {
            if count > maxCount {
                maxCount = count
            }
            count = 0
        }
    }
    if count > maxCount {
        maxCount = count
    }
    return maxCount
}

Algorithm

Поддерживайте счетчик для подсчета единиц и увеличивайте его на 1 при встрече единицы.

Когда встречаете ноль, используйте текущий счетчик единиц для нахождения максимального количества последовательных единиц на данный момент, затем сбросьте счетчик единиц на 0.

В конце return максимальное значение.

😎

Vacancies for this task

vagas ativas with overlapping task tags are mostradas.

Todas as vagas
Ainda não há vagas ativas.