n位二进制,求不包含连续1的二进制(n位)数字个数。

http://www.geeksforgeeks.org/count-number-binary-strings-without-consecutive-1s/

也可以令f[i],表示共i位的二进制数字中,不包含连续1的二进制(i位)数字的个数。

  f[i]的组成有两部分:

    最高位为1:只有当次高位为0,且满足条件的二进制数字个数,即 f[i-2]

    最高位为0:次高位为0或1且满足条件的数字个数,即f[i-1]

  得: f[i] = f[i-2] + f[i-1]

给定num,求1~num的数字中,求对应的二进制不包含连续1的数字个数。

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 binary representations do NOT containconsecutive 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

题目:

https://leetcode.com/contest/leetcode-weekly-contest-34/problems/non-negative-integers-without-consecutive-ones/

给定的数字num,转换成二进制,共n位。

首先由上一题的结论,得到f[i],1 <= i < n

假设数字num为从高位到低位B: bn-1, bn-2, ..., 3,2,1

   应该在比num小或相等的数字中,计算没有连续二进制1的数字的个数。

   从高位到低位,遍历B,如果遇到1,则对应位改变为0后,则肯定比原num小。

     如:10110,从高到低遍历二进制位

       第一个1,前缀变为0,剩下四位,比原来的num小,则满足条件的数字个数为f(4)

     第二个1,前缀变为100,剩下两位,100XX,比原来的num小,则满足条件的数字个数为f(2)

       第三个1,前缀变为1010,剩下一位,1010X,比原来的num小,则满足条件的数字个数为f(1)

       遍历过程中,当遇到两个连续的1时需要中止遍历。如原num为1XX11XXX,继续遍历找到后面的1并改变为0,得到的数字确实比num小,但不满足不存在连续1的条件。

     遍历过程中,考虑的都是比原num小的数,最后还需要判断一下原num是否符合条件。

    int findIntegers(int num) {
vector<int> b_num;
//int tmp = num;
while( num != )
{
b_num.push_back( num % );
num /= ;
}
int f[] = { };
f[] = ;
f[] = ;
int n = b_num.size();
for( int i = ; i < n; i++ )
{
f[i] = f[i-] + f[i-];
}
int ans = ; int has_d_one = false;
for( int i = n-; i >= ; i-- )
{
if( b_num[i] )
{
ans += f[i];
if( i < n- )
{
if( b_num[i+] )
{
has_d_one=true;
break;
}
} }
} //判断一下num自身是否含连续1
//if( !( tmp & (tmp<<1) ) )
//{
// ans++;
//}
if( !has_d_one )
{
ans++;
}
return ans;
}

Non-negative Integers without Consecutive Ones的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. [Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix

    // Code goes here function countNegative (M, n, m) { count = ; i = ; j = m - ; && i < n) ...

  5. 第十六周 Leetcode 600. Non-negative Integers without Consecutive Ones(HARD) 计数dp

    Leetcode600 很简单的一道计数题 给定整数n 求不大于n的正整数中 二进制表示没有连续的1的数字个数 在dp过程中只要保证不出现连续1以及大于n的情况即可. 所以设计按位dp[i][j]表示 ...

  6. Interleaving Positive and Negative Numbers

    Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...

  7. [LintCode] Interleaving Positive and Negative Numbers

    Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...

  8. Lintcode: Interleaving Positive and Negative Numbers 解题报告

    Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-pos ...

  9. SH Script Grammar

    http://linux.about.com/library/cmd/blcmdl1_sh.htm http://pubs.opengroup.org/onlinepubs/9699919799/ut ...

随机推荐

  1. UE 不生成.bak文件

    .bak文件是UE处理文件时自动备份的文件,可以取消备份这样就不会生成.bak文件了 菜单:高级-设置-文件处理-备份        应用和确定

  2. 用ADO操作数据库的方法步骤(ZT)

    http://www.cppblog.com/changshoumeng/articles/113437.html 学习ADO时总结的一些经验 用ADO操作数据库的方法步骤 ADO接口简介 ADO库包 ...

  3. jQuery图片延迟加载插件:jquery.lazyload

    ----------------------------------------------------------------------------------------------- clas ...

  4. GDI+ 实现透明水印和文字

    最近给<JPEG浏览缩放器>增加了水印功能,在设计的过程中,参考了网上的文章,但是发现文章使用的GDI+ API封装包不是我现在使用的那一套,目前DELPHI使用的GDI+ API封装包有 ...

  5. JavaScript中的setInterval用法

    setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象.可以使用本动作更新来自数据库的变量或更新时间显示.setInterval动作的语法格式如下:setInterval ...

  6. python 装饰器的缺点以及解决方法

    1.python装饰器的缺点 装饰器可以允许我们在不改变函数或犯方法的调用方式的情况下,添加额外的功能; 如下所示,我们要在中的方法之前增加装饰器check_is_admin,用来判断执行类的方法的用 ...

  7. Uni2D 入门 -- Animation Clip 和 Animation API

    转载 csdn kakashi8841 http://blog.csdn.net/kakashi8841/article/details/17599505 Animation Clip 一个anima ...

  8. 吴裕雄 oracle 管理数据表对象

  9. 学JS的心路历程-Promise(三)

    今天我们来说then一些特殊情况以及Promise.all()与Promise.race(). 我们都知道函式作为参数传入时,可以参照的方式传入,也能传入时执行拿回传值作使用: function us ...

  10. 升级tensorflow1.0到1.3,报错ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory Failed to load the native TensorFlow runtime.

    先定位问题,发现在 /usr/local/cuda/include/ /usr/local/cuda/lib64/ 下面只有 libcudnn.so.5 因此,只要下载cudnn6.*版本的文件分别覆 ...