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 ...
随机推荐
- Android 蓝牙 socket通信
Android中蓝牙模块的使用 使用蓝牙API,Android应用程序能够执行以下功能: 扫描其他蓝牙设备查询本地已经配对的蓝牙适配器建立RFCOMM通道通过服务发现来连接其他设备在设备间传输数据管理 ...
- gcc编译 汇编 选项
gcc生成main.out的步骤分解:<blockquote>main.c-----(-S 编译)-------->main.s-------(-c 汇编)------->ma ...
- 查找对应jar的maven包
当原有项目换成maven项目时,往往不知道具体jar包在maven里叫什么.这边文章教你如何去找到自己想要的jar的maven包. 工具/原料 浏览器 方法/步骤 1 登录一下网站 http: ...
- Django--django-admin.py on windows does not work
使用命令:Python django-admin.py startproject projectname 使用django-admin.py startapp myblog没有创建新的app,使用下面 ...
- yield关键字用法与解析(C# 参考)
yield 关键字向编译器指示它所在的方法是迭代器块. 编译器生成一个类来实现迭代器块中表示的行为. 在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值. 这是一个返 ...
- Linux 服务器的那些性能参数指标
一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤其当你的程序非正常工作的时候, ...
- <trim>: prefix+prefixOverrides+suffix+suffixOverrides
<trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...
- MATLAB 02
对一组数据作线性回归,并绘图 clear all; % 输入数据x和y x = [ ]'; y = [11 13 14 15 16 18 20 21 22 25 26 28 29 31]'; x = ...
- Linux Skills
++实现RedHat非正常关机的自动磁盘修复先登录到服务器,然后在/etc/sysconfig里增加一个文件autofsck,内容如下:AUTOFSCK_DEF_CHECK=yesPROMPT=yes ...
- springboot redis简单结合
参考: https://www.cnblogs.com/ityouknow/p/5748830.htmlhttp://blog.csdn.net/i_vic/article/details/53081 ...