LeetCode 717. 1-bit and 2-bit Characters
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.
Note:
1 <= len(bits) <= 1000
.bits[i]
is always0
or1
.
在这道题中,给定了两类特殊字符,一类是一位字符:0;一类是两位字符:10或11.
现给了一个由0和1组成的、且最后一位是0的数组,判断能否将其正确分割,且最后一位是单个字符0.
思路:如果出现1,则必须跟后面的0或1组成一个两位字符。因此从前往后遍历,如果bits[i] = 1,则将bits[i + 1]改为1;循环结束后,如果数组最后一位是1,则一定是一个两位字符,如果是0,则是一个一位字符。具体代码如下:
public class Solution {
public boolean isOneBitCharacter(int[] bits){
for(int i = 0; i < bits.length - 1; i++){
if(bits[i] == 1){
bits[i + 1] = 1;
i++;
}
}
return bits[bits.length - 1] == 0;
}
LeetCode 717. 1-bit and 2-bit Characters的更多相关文章
- 乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters
乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters 一.前言 在算法之中出现最多的就是字符串方面的问题了,关于字符串的 ...
- LeetCode 717. 1比特与2比特字符(1-bit and 2-bit Characters)
717. 1比特与2比特字符 LeetCode717. 1-bit and 2-bit Characters 题目描述 有两种特殊字符.第一种字符可以用一比特0来表示.第二种字符可以用两比特(10 或 ...
- leetcode 717. 1-bit and 2-bit Characters -easy
https://leetcode.com/problems/1-bit-and-2-bit-characters/description/ We have two special characters ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- 如何创建XHTML表单
首先奉上本菜曾经的作业——一张模仿智联招聘中的注册表单.虽然没有加样式显得很难看,但表单的基本要素已经具备了. 接下来进入正文,开始介绍各表单元素 form标签: 在创建表单时,第一步就是form标签 ...
- yum 出现error: db5 error
yum 安装k8s的过程中用了 Ctrl+ z, 然后yum 再也不能使用了: Error: rpmdb open failed 解决方法: rpm --rebuilddb yum clean all ...
- @RestController注解
@RestController注解其实就是@@Controller和@ResponseBody的组合:RESTFUL风格 看下源码: 当@ResponseBody放到Controller类上,改Con ...
- ubuntu lvm模式进行扩容
ubuntu的16.04 18.04有lvm的功能(安装的时候的选项),能在磁盘空间用完的时候,无缝增加一个磁盘进去.因为linux系统只有一个盘,所以是非常方便,不会有windows加1个磁盘要进行 ...
- facebook和twitter的截图分享
记录一下代码,以后自己可以抄..我抄我自己=.= SDK的接入看之前的博客.所以话不多少,直接上代码(记得分享一定要安装原生app(FB和twitter),不然只通过网页分享无法发图片.),初始化都在 ...
- TensorFlow中的卷积函数
前言 最近尝试看TensorFlow中Slim模块的代码,看的比较郁闷,所以试着写点小的代码,动手验证相关的操作,以增加直观性. 卷积函数 slim模块的conv2d函数,是二维卷积接口,顺着源代码可 ...
- 富文本(wangEditor框架)的使用教程
富文本的使用教程(wangEditor框架) 一,相信很多人用过很多富文本的框架,现在我向大家推荐一个很实用的一个富文本框架,具有丰富的功能项,容易使用. 所以本博客介绍这个富文本编辑器的使用哈!觉得 ...
- 通过NPM快速发布你的NodeJS模块(组件包)
1.更新 NPM - [ npm install -g npm | 该步骤可选:最好使用新版本] 楼主当前版本号 2.6.1 ,如果更新报错,可以尝试 国内淘宝镜像 $ npm -v 2.6.1 // ...
- 查询数据库时mapper报错:It's likely that neither a Result Type nor a Result Map was specified.
因为mapper.xml里把resultType写成了parameterType
- java 第一次实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:黎静 学号:20135338 成绩: 指导教师:娄嘉鹏 ...