1-bit and 2-bit Characters问题及解法
2017-10-30 08:55
239 查看
问题描述:
We have two special characters. The first character can be represented by one bit
second character can be represented by two bits (
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
示例:
问题分析:
每次记录最后出现的字符是否为只有0构成的,最终返回记录标识即可。
过程详见代码:
class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
bool res = 0;
for (int i = 0; i < bits.size();)
{
if (!bits[i])
{
i++;
res = 1;
}
else
{
i += 2;
res = 0;
}
}
return res;
}
};
We have two special characters. The first character can be represented by one bit
0. The
second character can be represented by two bits (
10or
11).
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
示例:
Input: bits = [1, 0, 0] Output: True Explanation: The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
Input: bits = [1, 1, 1, 0] Output: False Explanation: The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
问题分析:
每次记录最后出现的字符是否为只有0构成的,最终返回记录标识即可。
过程详见代码:
class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
bool res = 0;
for (int i = 0; i < bits.size();)
{
if (!bits[i])
{
i++;
res = 1;
}
else
{
i += 2;
res = 0;
}
}
return res;
}
};
相关文章推荐
- leetcode 717. 1-bit and 2-bit Characters 编码判断问题 + 贪心算法 + 遍历即可
- 隐马尔科夫模型,第三种问题解法,维比特算法(biterbi) algorithm python代码
- 多维算法思考(二):关于八皇后问题解法的探讨
- RMQ问题的ST、树状数组和线段树解法
- 字符串分割单词(word break problem)问题几种解法之比较
- LCA问题的ST,tarjan离线算法解法
- 最优化的几个问题的解法和比较之——无约束的非线性规划
- "3升5升得4升"——倒水问题的万能解法(扩展欧几里得算法)
- 约束优化问题数值解法
- LCA问题的ST,tarjan离线算法解法
- "中国象棋将帅问题"的一种解法
- 凸包问题之蛮力解法
- 【笔记】染色问题的俩解法
- ubuntu上编译安装apache步骤、遇到的一些问题解法
- 约瑟夫环问题(O(n)解法)
- 01背包问题(DFS解法)
- Power of Two问题及解法
- 迷宫问题之图深度优先遍历解法
- 背包问题-四种解法(穷举,递归,二维数组,一维数组)
- Combinations问题及解法