717. 1-bit and 2-bit Characters最后一位数是否为0
[抄题]:
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]:
[思维问题]:
没见过,不会
[一句话思路]:
正常操作后判断位数能不能对上
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 用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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- Swift2.0语言教程之函数的返回值与函数类型
Swift2.0语言教程之函数的返回值与函数类型 Swift2.0中函数的返回值 根据是否具有返回值,函数可以分为无返回值函数和有返回值函数.以下将会对这两种函数类型进行讲解. Swift2.0中具有 ...
- 深入浅出Redis-redis哨兵集群
1.Sentinel 哨兵 Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所 ...
- 前端福利!10个短小却超实用的JavaScript 代码段
JavaScript正变得越来越流行,它已经成为前端开发的第一选择,并且利用基于JavaScript语言的NodeJS,我们也可以开发出高 性能的后端服务,甚至我还看到在硬件编程领域也出现了JavaS ...
- CentOS 7.2.1511编译安装Nginx1.10.1+MySQL5.7.14+PHP7.0.11
准备篇 一.防火墙配置 CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.se ...
- test 2017-1-5
// dpm(drupal_get_filename('module','devel'));// sites/all/modules/contrib/dev/devel/devel.mod ...
- test 2016-12-28
// dpm(variable_get('node_submitted_page'));// //0// dpm(variable_get('language_count'));// //i3 = i ...
随机推荐
- python学习之准备
快速入门:十分钟学会Pythonhttp://python.jobbole.com/43922/python框架http://www.elias.cn/Python/HomePage#toc14[Py ...
- 使用python对文件中的数值进行累加
问题描述: 一个文件由若干条记录组成,记录的格式为:“num1 num2”,有时候,需要统计文件中num1对应的num2的总值.处理问题的思路 用传说中的python来处理,很方便.几行代码就可以了. ...
- MyEclipse2014安装操作步骤+破解
第一步 第二步 第三步 第四步 第五步 第六步 破解操作步骤 1.安装完成 MyEclipse2014(适用于 2013 等版本)后,不要打开软件.解压破解文件压缩包,得到一下文件列表:双击 run. ...
- C# 在类文件自动添加文件注释的方法
对于vs2013来讲, 步骤: 1.VS2013 中找到(安装盘符以C盘为例)C:\ProgramFiles(x86)\Microsoft VisualStudio12.0\Common7\IDE\I ...
- 转载 关于case语句的优先级
对于这样的组合逻辑电路 always@(X) case(X) X1: X2: …… endcase 如果分支项包含变量X的所有取值情况,并且互相不重复,那么这样的情况,其实没有必要使用综合指令. (一 ...
- ACM学习历程—51NOD 1770数数字(循环节)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1770 这是这次BSG白山极客挑战赛的A题.由于数字全部相同,乘上b必然会 ...
- jquery移除、绑定、触发元素事件
unbind(type [,data]) //data是要移除的函数 $('#btn').unbind("click"); //移除click $('#btn').unbind() ...
- webpack新版本4.12应用九(配置文件之configuration)
配置 查看原文|编辑此页 webpack 是需要传入一个配置对象(configuration object).取决于你如何使用 webpack,可以通过两种方式之一:终端或 Node.js.下面指定了 ...
- Delphi 连接 Paradox
应用TDataBase控件把DataBase的DriveName设为STANDARD,然后设置DataBase的Params中设置PATH=*.db文件地点目录DEFAULT DRIVER=PARAD ...
- 关于app集成支付宝应用内支付的问题总结
pem文件生成,将合作伙伴密钥复制到notepad++中,每45个字符回车,去除空格,头尾加上标题,文件需保存为无BOM的UTF8格式,就OK. 可以每行64个字符,共216个字符. 近来处理了 ...