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.

原题地址:  1-bit and 2-bit Characters

难度:  Easy

题意: 存在两类数,一类是10或者11,另一类为0.将一个数组拆分为这两类数,判断最后一组数为0,比如上面例子,第一组数为[1,0],第二组数为[0]

思路:

对于数字1,后面的一个数字是1或者0都行,所以遇到以可以跳过1后面的值,遇到0,则移动一位

代码:

class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
i = 0
while i < len(bits):
if bits[i] == 1:
i += 2
if i == len(bits):
return False
else:
i += 1
return True

时间复杂度: O(n)

空间复杂度: O(1)

最新文章

  1. Linux常用命令03
  2. Storm集成Kafka应用的开发
  3. CodeForces #367 div2 C
  4. Python成长笔记 - 基础篇 (三)python列表元组、字典、集合
  5. ruby -- 基础学习(三)设置中国时区时间
  6. smarty简单介绍
  7. 期权交易基本原理——买进看跌期权(Long Put),卖出看跌期权(Short Put)
  8. jQuery&amp;nbsp;Ajax&amp;nbsp;实例&amp;nbsp;全解析
  9. listview加载图片显示
  10. Hbuilder 常用快捷键汇总
  11. ES6模板字面量
  12. 在centos上面安装phantomjs
  13. Android Studio 每次运行都会再下载一遍,修改
  14. Java代码中解压RAR文件
  15. 和我一起学《HTTP权威指南》——安全HTTP与HTTPS
  16. 父页面操作嵌套iframe子页面的HTML标签元素
  17. Ubuntu关机时间过长,总是停在logo界面
  18. 使用scrapy爬取dota2贴吧数据并进行分析
  19. 【题解】 bzoj3105: [cqoi2013]新Nim游戏 (线性基+贪心)
  20. Android之针对WebView的全屏播放

热门文章

  1. IT兄弟连 JavaWeb教程 Servlet中定义的变量的作用域类型
  2. 【POJ - 2376】Cleaning Shifts(贪心)
  3. 天天坐在电脑面前,小心抑郁!来自一个人的旅行&lt;自导自演&gt;
  4. htm5 + ajax 文件上传
  5. usb被占用时,可以用这些方法进行adb无线调试
  6. sh 脚本报错
  7. Apache Kylin的架构特性
  8. Apache Cordova
  9. 关于setTimeout和Promise执行顺序问题
  10. Performance testing architecture