LeetCode—717

Approach

# 判斷一個數列扣掉結尾是0以外,前面由[0, 10, 11]這三個數字組成
Check whether a sequence consists only of the numbers 0, 10, and 11, except for the trailing 0.

Time Complexity

O(n)

Space Complexity

O(1)

Code

class Solution {
public:
    bool isOneBitCharacter(vector<int>& bits) {

        if (bits.size() == 1) {
            return true;
        }
        else {
            int l = bits.size();

            for (int i = 0 ; i < l - 1 ; ) {
                if (i == l - 2 && bits[i] == 1) {
                    return false;
                }

                if (bits[i] == 1) {
                    i += 2;
                }
                else {
                    i++;
                }
            }

            return true;
        }
    }
};