【Leetcode_easy】717. 1-bit and 2-bit Characters

problem

717. 1-bit and 2-bit Characters

题意:
solution1:

class Solution {
public:
    bool isOneBitCharacter(vector<int>& bits) {
       int i=0;
        while(i<bits.size()-1)
        {
            if(bits[i]==0) i+=1;
            else i+=2;
        }
        return i==bits.size()-1;
    }
};

solution2:根据数组的特性计算。

class Solution {
public:
    bool isOneBitCharacter(vector<int>& bits) {
       int i=0, n = bits.size();
        while(i<n-1)
        {
            //if(bits[i]==0) i+=1;
            //else i+=2;
            i += bits[i]+1;
        }
        return i==n-1;
    }
};

参考

1. Leetcode_easy_717. 1-bit and 2-bit Characters;

2. Grandyang;

原文地址:https://www.cnblogs.com/happyamyhope/p/11114840.html