From ad962f2376d5c1b84cad18cf2ae52d88f9e2366f Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Mon, 21 Feb 2022 01:46:43 +0800 Subject: [PATCH] add: 220220-CN [cpp] --- cpp/2202/220220-CN.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 cpp/2202/220220-CN.cpp diff --git a/cpp/2202/220220-CN.cpp b/cpp/2202/220220-CN.cpp new file mode 100644 index 0000000..e27458f --- /dev/null +++ b/cpp/2202/220220-CN.cpp @@ -0,0 +1,27 @@ +#include + +/** + * 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& 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; +}