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. 这题说数组中最后一个数字为0,问最后一个字符是不是one-bit-character。因为0就是one-bit-character,10是two-bit-character。所以判断最后一个数字0前面连续1有多少个,
class Solution {
public boolean isOneBitCharacter(int[] bits) {
int len=bits.length;
if(len==1)
return true; if(bits[len-2]==0)
return true;
else{
int k=1;//从倒数第二个字符开始,连续1的个数
int i=len-3;
while(i>=0){
if(bits[i]==1)
k++;
else
break;
i--;
}
if(k%2==0)
return true;
else
return false;
}
}
}
1-bit and 2-bit Characters的更多相关文章
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- [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] Read N Characters Given Read4 用Read4来读取N个字符
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- 求解n皇后
要求:在国际象棋上摆放n个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法 思路:很直观的想法就是在棋盘上一个一个皇后的摆,如果冲突,则摆放在另一个位置,直至 ...
- memcached 详解
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- C++11:使用 auto/decltype/result_of使代码可读易维护
C++11 终于加入了自动类型推导.以前,我们不得不使用Boost的相关组件来实现,现在,我们可以使用"原生态"的自动类型推导了! C++引入自动的类型推导,并不是在向动态语言(强 ...
- 看看腾讯是怎么做产品设计分析的 - 腾讯QQ音乐业务产品规划
- 网站开发进阶(二十八)初探localStorage
初探localStorage 注: localStorage经典项目应用案例 HTML5中提供了localStorage对象可以将数据长期保存在客户端,直到人为清除. localStora ...
- 《java入门第一季》之面向对象(抽象类到底多抽象)
/* 抽象类的概述: 动物不应该定义为具体的东西,而且动物中的吃,睡等也不应该是具体的. 我们把一个不是具体的功能称为抽象的功能,而一个类中如果有抽象的功能,该类必须被称为是抽象类. 抽象类的特点: ...
- Java进阶(二十二)使用FileOutputStream写入文件
Java使用FileOutputStream写入文件 绪 在Java中,文件输出流是一种用于处理原始二进制数据的字节流类.为了将数据写入到文件中,必须将数据转换为字节,并保存到文件.请参阅下面的完整的 ...
- Android群英传笔记——第九章:Android系统信息和安全机制
Android群英传笔记--第九章:Android系统信息和安全机制 本书也正式的进入尾声了,在android的世界了,不同的软件,硬件信息就像一个国家的经济水平,军事水平,不同的配置参数,代表着一个 ...
- Leetcode_116_Populating Next Right Pointers in Each Node
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43532817 Given a binary tree st ...
- SSRS2008中控件ID冲突问题
[BC30179] class"textbox21_TextBoxExprHost"和 class"Textbox21_TextBoxExprHost"在 cl ...