JavaRush /Java блог /Архив info.javarush /Кухня(); Задание N62
terranum
28 уровень
Milan

Кухня(); Задание N62

Статья из группы Архив info.javarush
Кухня(); Задание N62 - 1 Правила [Одномерные массивы] 62. Дан массив А. Найти длину самой длинной последовательности подряд идущих элементов массива, равных нулю.
Комментарии (11)
ЧТОБЫ ПОСМОТРЕТЬ ВСЕ КОММЕНТАРИИ ИЛИ ОСТАВИТЬ КОММЕНТАРИЙ,
ПЕРЕЙДИТЕ В ПОЛНУЮ ВЕРСИЮ
Airon Уровень 34
30 сентября 2014
Раздуваете из мухи слона…
public static int maxLength0(int... array) {
    int countMax = 0, count = 0;
    for(int x : array) {
        count = x == 0 ? ++count : 0;
        countMax = Math.max(count, countMax);
    }
    return countMax;
}
Voronblack Уровень 17
30 сентября 2014
Не очень изящно и немного громоздко, но работоспособно
public static int getMaxZeroSequence(int...arr)
    {
        int sequence = 0;
        int count = 0;

        if (arr.length == 0 )
            throw new IllegalArgumentException("Bad args");

        for(int i = 0; i < arr.length; i++)

            if (arr[i] == 0)
            {
                count = 1;

                while((i + 1!= arr.length) && (arr[i+1] == 0))
                {
                    count++;
                    i++;
                }

                sequence = sequence < count ? count : sequence;
            }

        return sequence;
    }
Vash_the_Stampede Уровень 11
30 сентября 2014
public static int solve(int[] arr) {
    int count = 0;
    int max = 0;
    for (int i : arr) {
        try {
            i /= i;
            count = 0;
        } catch (ArithmeticException e) {
            count++;
        } finally {
            max = Math.max(max, count);
        }
    }
    return max;
}