leetcode (1-bit and 2-bit Characters)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/84726175

Title:1-bit and 2-bit Characters    717

Difficulty:Easy

原题leetcode地址:https://leetcode.com/problems/1-bit-and-2-bit-characters/

1. 遍历数组,如果是1,则加2,如果是0,则加1;

时间复杂度:O(n),一次一层for循环,需要遍历整个数组。

空间复杂度:O(1),没有申请额外的空间。

    /**
     * 遍历数组,如果是1,则加2,如果是0,则加1;(注意是1的时候,必须加2)
     * @param bits
     * @return
     */
    public static boolean isOneBitCharacter(int[] bits) {

        int i = 0;

        while (i < bits.length - 1) {
            if (bits[i] == 1) {
                i += 2;
            }
            else {
                i++;
            }
        }

        return i == bits.length - 1;

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/84726175