[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

没见过,不会

[一句话思路]:

正常操作后判断位数能不能对上

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 用while循环,因为最后要差也就之差了一位数

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

正常操作后判断位数能不能对上

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

直接用能不能对上来返回:

//return, check
return i == n - 1;

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public boolean isOneBitCharacter(int[] bits) {
//ini
int n = bits.length;
int i = 0; //cc
if (bits == null || n == 0) {
return false;
} //while loop
while (i < n - 1) {
if (bits[i] == 0) {
i++;
}else {
i += 2;
}
} //return, check
return i == n - 1;
}
}

717. 1-bit and 2-bit Characters最后一位数是否为0的更多相关文章

  1. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  2. [LeetCode#157] Read N Characters Given Read4

    Problem: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is ...

  3. linux导出Excel The maximum column width for an individual cell is 255 characters

    linux环境到处Excel报错: The maximum column width for an individual cell is 255 characters 解决方案: for (int i ...

  4. Swift2.0语言教程之函数的返回值与函数类型

    Swift2.0语言教程之函数的返回值与函数类型 Swift2.0中函数的返回值 根据是否具有返回值,函数可以分为无返回值函数和有返回值函数.以下将会对这两种函数类型进行讲解. Swift2.0中具有 ...

  5. 深入浅出Redis-redis哨兵集群

    1.Sentinel 哨兵 Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所 ...

  6. 前端福利!10个短小却超实用的JavaScript 代码段

    JavaScript正变得越来越流行,它已经成为前端开发的第一选择,并且利用基于JavaScript语言的NodeJS,我们也可以开发出高 性能的后端服务,甚至我还看到在硬件编程领域也出现了JavaS ...

  7. CentOS 7.2.1511编译安装Nginx1.10.1+MySQL5.7.14+PHP7.0.11

    准备篇 一.防火墙配置 CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.se ...

  8. test 2017-1-5

    //    dpm(drupal_get_filename('module','devel'));//    sites/all/modules/contrib/dev/devel/devel.mod ...

  9. test 2016-12-28

    // dpm(variable_get('node_submitted_page'));// //0// dpm(variable_get('language_count'));// //i3 = i ...

随机推荐

  1. freemarker 常见问题

    <#setting date_format="yyyy-MM-dd"> ..设置时间格式然后获取从后台获取值${s.createTime?date}这样就能正常显示了 ...

  2. Windows下运行Hadoop

    Windows下运行Hadoop,通常有两种方式:一种是用VM方式安装一个Linux操作系统,这样基本可以实现全Linux环境的Hadoop运行:另一种是通过Cygwin模拟Linux环境.后者的好处 ...

  3. hibernate 一对一(One-to-One)

    一对一(one-to-one)实例(Person-IdCard) 一对一的关系在数据库中表示为主外关系.例如.人和身份证的关系.每个人都对应一个身份证号.我们应该两个表.一个是关于人信息的表(Pers ...

  4. 用python写定时任务

    一个是sched模块,一个是threading模块 参考链接:http://www.cnblogs.com/LinTeX9527/p/6181523.html

  5. 记一次RESTful调试过程

    1. 为什么前台怎么调用后台,都是跳到页面不存在. 因为已经改为RESTful,保存按钮的type还是submit: 2. 改成buttong之后,设置onclick="update()“, ...

  6. hbase官方文档(转)

    FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Soft ...

  7. form表单中name和id区别

    HTML文本是由HTML命令组成的描述性文本,HTML命令可以说明文字.图形.动画.声音.表格.链接等.HTML的结构包括头部(Head).主体(Body)两大部分,其中头部描述浏览器所需的信息,而主 ...

  8. 【转】使用JMeter测试你的EJB

    对EJB进行一些性能基准测试是非常有必要和有帮助的,测试的方法和工具有很多,不过我最近发现,Apache JMeter是进行基准测试的一个优秀工具.可惜的是,JMeter没有提供一个可测试任意EJB的 ...

  9. python下载指定页面的所有图片

    实现步骤: 1.下载页面源码 2.对页面进行解析,获取页面中所有的图片路径 3.下载图片到指定路径 代码实例: # coding: utf-8 import urllib2 # 该模块用于打开页面地址 ...

  10. js作用域和作用域链

    作用域 首先要知道一点,js跟python都是一样的,先编译后执行 唯一的区别在于,js对于只声明,未定义的变量,报错是未定义,后面代码会有这个例子 编译的过程只有赋值和开辟空间的过程 只有函数,类才 ...