add: 220220-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-02-21 01:46:43 +08:00
parent ec3020b2dc
commit ad962f2376
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 27 additions and 0 deletions

27
cpp/2202/220220-CN.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <vector>
/**
* 717. 1-bit and 2-bit Characters
* We have two special characters:
* The first character can be represented by one bit 0.
* The second character can be represented by two bits (10 or 11).
* Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.
*/
class Solution {
public:
static inline bool isOneBitCharacter(const std::vector<int>& bits) {
int n = bits.size();
if (n == 1 || bits[n - 2] == 0)
return true;
bool vis[1000] {true};
for (int i = 0; i < n - 2; ++i)
if (vis[i])
vis[1 + bits[i] + i] = true;
return !vis[n - 2];
}
};
int main() {
return 0;
}