问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3740 访问。

有两种特殊字符。第一种字符可以用一比特0来表示。第二种字符可以用两比特(10 或 11)来表示。

现给一个由若干比特组成的字符串。问最后一个字符是否必定为一个一比特字符。给定的字符串总是由0结束。

输入: bits = [1, 0, 0]

输出: True

解释: 唯一的编码方式是一个两比特字符和一个一比特字符。所以最后一个字符是一比特字符。

输入: bits = [1, 1, 1, 0]

输出: False

解释: 唯一的编码方式是两比特字符和两比特字符。所以最后一个字符不是一比特字符。

注意:

1 <= len(bits) <= 1000.

bits[i] 总是0 或 1.


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.

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.

Note:

1 <= len(bits) <= 1000.

bits[i] is always 0 or 1.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3740 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 1, 1, 1, 0 };
var res = IsOneBitCharacter(nums);
Console.WriteLine(res); Console.ReadKey();
} private static bool IsOneBitCharacter(int[] bits) {
//1总是要和后面的1个数字编码,即+2,0不用编码,+1往后继续即可
//这道题做不出来主动面壁思过吧
int i = 0;
while(i < bits.Length - 1) {
i = bits[i] == 0 ? ++i : i + 2;
}
return i == bits.Length - 1;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3740 访问。

False

分析:

显而易见,以上算法的时间复杂度为:  。

最新文章

  1. PHP使用JSON通信
  2. JavaScript作用域原理(二)——预编译
  3. IE比Chrome强的一个地方
  4. CentOS下yum安装wine
  5. Fedora 17下交叉编译vlc-2.0.6-win32小记
  6. (翻译玩)在使用flask-script的应用上使用gunicorn
  7. HBase 6、用Phoenix Java api操作HBase
  8. 【基础】新手任务,五分钟全面掌握JQuery选择器
  9. Python打包分发工具setuptools
  10. 分布式改造剧集之Redis缓存采坑记
  11. 【洛谷P1303A*Bprublem】
  12. 如何使用spring配合mybatis配置多个数据源并应用?
  13. 【托业】【新托业TOEIC新题型真题】学习笔记5-题库二-&gt;P7
  14. XML系列之--对电文格式XML的简单操作(三)
  15. git小技巧--如何从其他分支merge个别文件或文件夹
  16. pandas的离散化,面元划分
  17. 【2019北京集训2】duck 线段树优化建图+tarjan
  18. 193 Valid Phone Numbers
  19. UML类图和时序图
  20. (jsp/html)网页上嵌入播放器(常用播放器代码整理) http://www.jb51.net/article/37267.htm

热门文章

  1. Host是什么?如何设置host文件?
  2. less : 写一个display:flex的mixin
  3. 发布一个自己做的图片转Base64的软件,Markdown写文章时能用到
  4. 循序渐进nginx(二):反向代理、负载均衡、缓存服务、静态资源访问
  5. SpringBoot 整合Mybatis + PageHelper 实现分页
  6. 小书MybatisPlus第7篇-代码生成器的原理精讲及使用方法
  7. 为什么不应该使用goroutine id?
  8. 16 . Go之网络编程
  9. java基础(一)注释
  10. 枚举-称硬币POJ1013