485. Max Consecutive Ones - LeetCode

Question

485. Max Consecutive Onesjava

Solution

題目大意:給一個數組,取連續1的最大長度數組

思路:遍歷數組,連續1就加1,取最大code

Java實現:ip

public int findMaxConsecutiveOnes(int[] nums) {
    if (nums == null) return 0;
    int result = 0;
    int tmp = 0;
    for (int i : nums) {
        if (i == 1) {
            tmp++;
        } else {
            result = tmp > result? tmp: result;
            tmp = 0;
        }
    }
    result = tmp > result? tmp: result;
    return result;
}
相關文章
相關標籤/搜索