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 ...
随机推荐
- git 生成公钥与私钥
一. 第1步:创建SSH Key.在windows下查看[c盘->用户->自己的用户名->.ssh]下是否有id_rsa.id_rsa.pub文件,如果没有需要手动生成.打开git ...
- CodeForces - 622F:The Sum of the k-th Powers (拉格朗日插值法求自然数幂和)
There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees. ...
- DbEntry 默认 主键ID为long
DbEntry 默认 主键ID为long,如果自己表中的主键ID为int,可以通过以下方式修改: public class Company :DbObjectModel<Company,int& ...
- matlab 与 modelsim 联调 cic抽取滤波器
注:本设计的参数为:D=2,R=5,N=3:时钟频率为50mhz,输入信号为有符号8位,根据公式bmax=bin+N*log(2,R*D):可以得到bmax=18: 1,cic抽取滤波器原理 网上资料 ...
- vue-cli结构
|-- build // 项目构建(webpack)相关代码 | |-- build.js // 生产环境构建代码 | |-- check-version.js // 检查node.npm等版本 | ...
- 程序4-2 access函数实例
//http://blog.chinaunix.net/uid-24549279-id-71355.html /* ========================================== ...
- .NET泛型与非泛型的问题
泛型集合通常情况下,建议您使用泛型集合,因为这样可以获得类型安全的直接优点而不需要从基集合类型派生并实现类型特定的成员.下面的泛型类型对应于现有的集合类型:1.List 是对应于 ArrayList ...
- 读写另一EXE的内存
procedure TfrmMain.tmrChangeServerNameTimer(Sender: TObject); function GetProcessID(FileName: string ...
- 你知道PING功能是怎么实现的吗
以太网的协议有层,而每层都包含有更多的协议.所谓协议,通俗的讲就是通信双方约定的规则. 今天我们介绍一些一个听起来陌生却有很常用的协议,ICMP协议. ICMP是(Internet Control ...
- 1133 Splitting A Linked List
题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...