【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;

posted on 2019-07-01 16:26  鹅要长大  阅读(145)  评论(0编辑  收藏  举报

导航