forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
47 lines (36 loc) · 1015 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// Source : https://leetcode.com/problems/1-bit-and-2-bit-characters/description/
/// Author : liuyubobobo
/// Time : 2017-10-28
#include <iostream>
#include <vector>
using namespace std;
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int i;
for(i = 0 ; i < bits.size() ; )
if(i == bits.size() - 1)
return true;
else{
if(bits[i] == 0)
i += 1;
else
i += 2;
}
return false;
}
};
void printBool(bool res){
cout << (res ? "true" : "false") << endl;
}
int main() {
int bits1[] = {1, 0, 0};
vector<int> vec1(bits1, bits1 + sizeof(bits1)/sizeof(int));
printBool(Solution().isOneBitCharacter(vec1));
int bits2[] = {1, 1, 1, 0};
vector<int> vec2(bits2, bits2 + sizeof(bits2)/sizeof(int));
printBool(Solution().isOneBitCharacter(vec2));
return 0;
}