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 always0or1.
在这道题中,给定了两类特殊字符,一类是一位字符: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 ...
随机推荐
- asp.net core发布到docker报Microsoft.ApplicationInsights.AspNetCore miss的错误
dotnet core 2.1的asp.net core在docker下部署的时候发生下面的错误. Error: An assembly specified in the application de ...
- Altium软件中Unknowpin的详细解决办法
1.Altium软件中Unknowpin第一种原因:PCB封装缺失遗漏,直接加入对应的封装即可.点击箭头指示处的Add,接着点击OK之后,再点击图中所示处Browse...选择封装库的封装即可. 2. ...
- SourceTree跳过注册安装使用
%LocalAppData%\Atlassian\SourceTree\目录 创建一个accounts.json [ { "$id": "1", ...
- 学习java常见dos命令
在java基础学习阶段时一般会在dos命令行下操作文件,这里列出一些常用命令. 打开DOS控制台的方式 按win+r 再输入 cmd 然后回车. 常用DOS命令 d: 回车 盘符切换 (盘符加冒号) ...
- happybase(TSocket read 0 bytes)
关于报错happybase 是使用python连接hbase的一个第三方库,目前基于thrift1 .在使用过程中经常碰到报错 TTransportException(type=4, message= ...
- Netty源码分析第7章(编码器和写数据)---->第2节: MessageToByteEncoder
Netty源码分析第七章: Netty源码分析 第二节: MessageToByteEncoder 同解码器一样, 编码器中也有一个抽象类叫MessageToByteEncoder, 其中定义了编码器 ...
- Apache 性能配置优化
前言 最近在进行apache性能优化设置.在修改apache配置)文件之前需要备份原有的配置文件夹conf,这是网站架设的好习惯.以下的apache配置调优均是在red had的环境下进行的. htt ...
- Trait 是什么东西
PHP官方手册里面写的内容是 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少 ...
- Windows环境下,从零开始搭建Nodejs+Express+Ejs框架(二)---安装Express,ejs
安装Express,ejs的前提是一定要先安装nodejs,具体安装方法请查看 http://www.cnblogs.com/tfiremeteor/p/8973105.html 安装Express和 ...
- find 删除文件
find 目录 -type f -name '*' -print0 | xargs -0 rm