[LeetCode] Non-negative Integers without Consecutive Ones 非负整数不包括连续的1
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones.
Example 1:
Input: 5
Output: 5
Explanation:
Here are the non-negative integers <= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
Note: 1 <= n <= 109
这道题给了我们一个数字,让我们求不大于这个数字的所有数字中,其二进制的表示形式中没有连续1的个数。根据题目中的例子也不难理解题意。我们首先来考虑二进制的情况,对于1来说,有0和1两种,对于11来说,有00,01,10,三种情况,那么有没有规律可寻呢,其实是有的,我们可以参见这个帖子,这样我们就可以通过DP的方法求出长度为k的二进制数的无连续1的数字个数。由于题目给我们的并不是一个二进制数的长度,而是一个二进制数,比如100,如果我们按长度为3的情况计算无连续1点个数个数,就会多计算101这种情况。所以我们的目标是要将大于num的情况去掉。下面从头来分析代码,首先我们要把十进制数转为二进制数,将二进制数存在一个字符串中,并统计字符串的长度。然后我们利用这个帖子中的方法,计算该字符串长度的二进制数所有无连续1的数字个数,然后我们从倒数第二个字符开始往前遍历这个二进制数字符串,如果当前字符和后面一个位置的字符均为1,说明我们并没有多计算任何情况,不明白的可以带例子来看。如果当前字符和后面一个位置的字符均为0,说明我们有多计算一些情况,就像之前举的100这个例子,我们就多算了101这种情况。我们怎么确定多了多少种情况呢,假如给我们的数字是8,二进制为1000,我们首先按长度为4算出所有情况,共8种。仔细观察我们十进制转为二进制字符串的写法,发现转换结果跟真实的二进制数翻转了一下,所以我们的t为"0001",那么我们从倒数第二位开始往前遍历,到i=1时,发现有两个连续的0出现,那么i=1这个位置上能出现1的次数,就到one数组中去找,那么我们减去1,减去的就是0101这种情况,再往前遍历,i=0时,又发现两个连续0,那么i=0这个位置上能出1的次数也到one数组中去找,我们再减去1,减去的是1001这种情况,参见代码如下:
解法一:
class Solution {
public:
int findIntegers(int num) {
int cnt = , n = num;
string t = "";
while (n > ) {
++cnt;
t += (n & ) ? "" : "";
n >>= ;
}
vector<int> zero(cnt), one(cnt);
zero[] = ; one[] = ;
for (int i = ; i < cnt; ++i) {
zero[i] = zero[i - ] + one[i - ];
one[i] = zero[i - ];
}
int res = zero[cnt - ] + one[cnt - ];
for (int i = cnt - ; i >= ; --i) {
if (t[i] == '' && t[i + ] == '') break;
if (t[i] == '' && t[i + ] == '') res -= one[i];
}
return res;
}
};
下面这种解法其实蛮有意思的,其实长度为k的二进制数字符串没有连续的1的个数是一个斐波那契数列f(k)。比如当k=5时,二进制数的范围是00000-11111,我们可以将其分为两个部分,00000-01111和10000-10111,因为任何大于11000的数字都是不成立的,因为有开头已经有了两个连续1。而我们发现其实00000-01111就是f(4),而10000-10111就是f(3),所以f(5) = f(4) + f(3),这就是一个斐波那契数列啦。那么我们要做的首先就是建立一个这个数组,方便之后直接查值。我们从给定数字的最高位开始遍历,如果某一位是1,后面有k位,就加上f(k),因为如果我们把当前位变成0,那么后面k位就可以直接从斐波那契数列中取值了。然后标记pre为1,再往下遍历,如果遇到0位,则pre标记为0。如果当前位是1,pre也是1,那么直接返回结果。最后循环退出后我们要加上数字本身这种情况,参见代码如下:
解法二:
class Solution {
public:
int findIntegers(int num) {
int res = , k = , pre = ;
vector<int> f(, );
f[] = ; f[] = ;
for (int i = ; i < ; ++i) {
f[i] = f[i - ] + f[i - ];
}
while (k >= ) {
if (num & ( << k)) {
res += f[k];
if (pre) return res;
pre = ;
} else pre = ;
--k;
}
return res + ;
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/90571/java-solution-dp
https://discuss.leetcode.com/topic/90639/c-non-dp-o-32-fibonacci-solution
https://discuss.leetcode.com/topic/90671/java-o-1-time-o-1-space-dp-solution
http://www.geeksforgeeks.org/count-number-binary-strings-without-consecutive-1s/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Non-negative Integers without Consecutive Ones 非负整数不包括连续的1的更多相关文章
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- 第十六周 Leetcode 600. Non-negative Integers without Consecutive Ones(HARD) 计数dp
Leetcode600 很简单的一道计数题 给定整数n 求不大于n的正整数中 二进制表示没有连续的1的数字个数 在dp过程中只要保证不出现连续1以及大于n的情况即可. 所以设计按位dp[i][j]表示 ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- Non-negative Integers without Consecutive Ones
n位二进制,求不包含连续1的二进制(n位)数字个数. http://www.geeksforgeeks.org/count-number-binary-strings-without-consecut ...
- LeetCode--Longest Consecutive Sequence(最长连续序列) Python
题目描述: Longest Consecutive Sequence(最长连续序列) 中文: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 英文: Given ...
- [Swift]LeetCode600. 不含连续1的非负整数 | Non-negative Integers without Consecutive Ones
Given a positive integer n, find the number of non-negativeintegers less than or equal to n, whose b ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 600. Non-negative Integers without Consecutive Ones
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose ...
随机推荐
- Catch That Cow_bfs
Catch That Cow 题目大意:FrammerJohn找奶牛,给出n和k.FJ在n处.每次他可以向左移动一格.向右移动一格或者移动到自己当前格子数乘2的地方.求FJ最少移动多少次.其中,FJ和 ...
- 用python程序来画花
from turtle import * import time setup(600,800,0,0) speed(0) penup() seth(90) fd(340) seth(0) pendow ...
- JVM活学活用——优化springboot
介绍 在SpringBoot的Web项目中,默认采用的是内置Tomcat,当然也可以配置支持内置的jetty,内置有什么好处呢? 1. 方便微服务部署. 2. 方便项目启动,不需要下载Tomcat或者 ...
- [福大软工] W班 总成绩排行榜
评分链接 作业1 作业2 作业3 作业4 总分排名
- Beta冲刺NO.2
Beta冲刺 第二天 1.昨天的问题 由于前面的冲刺留下的问题很多,而且混乱的代码给我们接下来的完善工作带来了巨大的困难. 2. 今天解决的进度 潘伟靖: 1.对代码进行了review 2.为系统增加 ...
- VS2005 与虚拟机的那点事
好不容易把VS2008装上了,每次F5编译的时候,程序自动退出,意外的是VS2005也是同样的结果.好在有像我一样的好心人,愿意把解决的方法与大家共享. 经过搜索找到了答案,原来是VMwa ...
- XCode Build Settings中几种Search Paths
Header search path:去查找头文件的路径,同在在你需要使用第三方库的时候,在这里设置你的头文件路径目录,如图 <code><span class="str& ...
- 第二篇:利用shell脚本执行webservice请求——基于soap
1. 项目背景 以往我们在开发基于webservice的项目中,我们总习惯于直接使用webservice的一些框架,如Axis,axis2和Xfire等.框架的好处是将webservice所涉及到的s ...
- nyoj 孪生素数
孪生素数问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 写一个程序,找出给出素数范围内的所有孪生素数的组数.一般来说,孪生素数就是指两个素数距离为2,近的不能再 ...
- LeetCode & Q38-Count and Say-Easy
String Description: The count-and-say sequence is the sequence of integers with the first five terms ...