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).

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.

Example 1:

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.

Example 2:

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.

Note:

  • 1 <= len(bits) <= 1000.
  • bits[i] is always 0 or 1.

这道题说有两种特殊的字符,一种是两位字符,只能是二进制的11和10,另一种是单个位字符,只能是二进制的0。现在给了我们一个只包含0和1的数组,问我们能否将其正确的分割,使得最后一个字符是个单个位字符。这道题可以使用贪婪算法来做,因为两种字符互不干扰,只要我们遍历到了数字1,那么其必定是两位字符,所以后面一位也得跟着,而遍历到了数字0,那么就必定是单个位字符。所以我们可以用一个变量i来记录当前遍历到的位置,如果遇到了0,那么i自增1,如果遇到了1,那么i自增2,我们循环的条件是i < n-1,即留出最后一位,所以当循环退出后,当i正好停留在n-1上,说明最后一位是单独分割开的,因为题目中限定了最后一位一定是0,所以没必要再判断了,参见代码如下:

解法一:

class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int n = bits.size(), i = ;
while (i < n - ) {
if (bits[i] == ) ++i;
else i+= ;
}
return i == n - ;
}
};

下面这种解法写的更加简洁了,直接用一行代替了if..else..语句,相当巧妙,当bits[i]为0时,i还是相当于自增了1,当bits[i]为1时,i相当于自增了2,最后还是在循环跳出后检测i是否为n-1,参见代码如下:

解法二:

class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
int n = bits.size(), i = ;
while (i < n - ) {
i += bits[i] + ;
}
return i == n - ;
}
};

下面我们来看递归解法,用的是回溯的思想,首先判断如果bits为空了,直接返回false,因为题目初始给的bits是非空的,在调用递归函数中为空了说明最后一位跟倒数第二位组成了个两位字符,所以不合题意返回false。再判断如果bits大小为1了,那么返回这个数字是否为0,其实直接返回true也行,因为题目中说了最后一个数字一定是0。然后我们新建一个数组t,如果bits的首元素为0,则我们的t赋值为去掉首元素的bits数组;如果bits的首元素是1,则我们的t服之为去掉前两个元素的bits数组,然后返回调用递归函数的结果即可,参见代码如下:

解法三:

class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
if (bits.empty()) return false;
if (bits.size() == ) return bits[] == ;
vector<int> t;
if (bits[] == ) {
t = vector<int>(bits.begin() + , bits.end());
} else if (bits[] == ) {
t = vector<int>(bits.begin() + , bits.end());
}
return isOneBitCharacter(t);
}
};

下面这种解法也是用的递归,递归函数用的不是原函数,这样可以只用位置变量idx来遍历,而不用新建数组t,初始时idx传入0,在递归函数中,如果idx为n了,相当于上面解法中的bits数组为空了情况,返回false;如果idx为n-1,返回true;如果bits[idx]为0,则返回调用递归函数的结果,此时idx加上1;如果bits[idx]为1,则返回调用递归函数的结果,此时idx加上2,参见代码如下:

解法四:

class Solution {
public:
bool isOneBitCharacter(vector<int>& bits) {
return helper(bits, );
}
bool helper(vector<int>& bits, int idx) {
int n = bits.size();
if (idx == n) return false;
if (idx == n - ) return bits[idx] == ;
if (bits[idx] == ) return helper(bits, idx + );
return helper(bits, idx + );
}
};

类似题目:

Gray Code

参考资料:

https://discuss.leetcode.com/topic/108743/java-solution-1-or-2

https://discuss.leetcode.com/topic/108766/c-both-iterative-and-recursive-solutions

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 1-bit and 2-bit Characters 一位和两位字符的更多相关文章

  1. LeetCode算法题-1-bit and 2-bit Characters(Java实现)

    这是悦乐书的第302次更新,第321篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第170题(顺位题号是717).有两个特殊字符,第一个字符可以用一个比特0表示,第二个字 ...

  2. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  3. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  5. 【LeetCode】717. 1-bit and 2-bit Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

  6. [leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串

    Given a string s , find the length of the longest substring t  that contains at most 2 distinct char ...

  7. LeetCode初级算法--字符串02:字符串中的第一个唯一字符

    LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...

  8. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  9. [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

随机推荐

  1. C语言的字符串操作函数小结

    一.strcat()函数 char *strcat(char *dest, const char *src) 把 src 所指向的字符串追加到 dest 所指向的字符串的结尾.它返回一个指向字符串de ...

  2. 1. 初识 Lucene

    在学习Lucene之前呢,我们当然首先要了解下什么是Lucene. 0x01 什么是Lucene ? Lucene是一套用于全文检索和搜索的开放源代码程序库,由Apache软件基金会支持和提供. Lu ...

  3. js面向对象的理解

    ECMAScript 有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是,ECMAScrip ...

  4. SSH相关知识

    SSH(Secure Shell, 安全Shell协议)是一种加密的网络传输协议,经常用于安全的远程登录. SSH只是一种协议,可以有多种实现. OPENSSH是一种应用广泛的实现. sshd是dae ...

  5. alpha-咸鱼冲刺day4

    一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 QAQ具体工作量没啥进展.但是前后端终于可以数据交互了!.. 四,问题困难 日常啥都不会,百度真心玩一年. 还得自学nodejs ...

  6. Java的暑期作业

    Java暑期作业 一.<恶意>读书笔记 <恶意>是日本作家东野圭吾写的推理小说之一.看完后不禁为东野先生的奇特的写作手法以及书中所展现的人性的丑恶所震撼.我认为这本书相较< ...

  7. 异步协程 的 trip库

    import trip headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, ...

  8. 解析与动作联动得SDN数据平面

    一种解析与执行联动的SDN可编程数据平面 现有问题和目标 在传统协议处理方式中,各层的协议类型和组合方式固定,使得添加或修改协议很困难(因为需要修改网络设备的解析模式) 基于解析和执行联动结构的可编程 ...

  9. 201421123042 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承中的关键词:Soper,object,override,project, 1.2 尝试使用思维导图将这些关键词组织起来.注: ...

  10. windows系统下安装 node.js (node.js安装及环境配置)

    node.js简介 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node. ...