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

 

Approach #1: DP. [C++]

class Solution {
public:
int findIntegers(int num) {
vector<int> f(35, 0);
f[0] = 1;
f[1] = 2;
for (int i = 2; i < 32; ++i)
f[i] = f[i-1] + f[i-2];
int ans = 0, k = 30, pre_bit = 0;
while (k >= 0) {
if (num & (1 << k)) {
ans += f[k];
if (pre_bit == 1) return ans;
pre_bit = 1;
} else pre_bit = 0;
k--;
}
return ans+1;
}
};

  

Analysis:

The solution if based on 2 fact:

First: the number of length k string without consecutive 1 is Fibonacci sequence f(k);

For example, is k = 5, the range is 00000 - 11111. We can consider it as two ranges, which are 00000 - 01111 ans 10000  10111. any number >= 11000 is not allowed due to consecutive 1. The first case is actually f(4), and the second case is f(3), so f(5) = f(4) + f(3).

Second: Scan the number from most significant digit, i.e. left to right, in binary format. If we find a '1' with k digits to the right, count increases by f(k) beause we can put a '0' at this digit and any valid length k string behind; After that, we continue the loop to consider the remaining case, i.e. we put a '1' at this digit. If consecutive 1s are found, we exit the loop and return the answer. By the end of the loop, we return ans + 1 to include the number n itself.

Reference:

https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/discuss/103754/C%2B%2B-Non-DP-O(32)-Fibonacci-solution

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

  1. Non-negative Integers without Consecutive Ones

    n位二进制,求不包含连续1的二进制(n位)数字个数. http://www.geeksforgeeks.org/count-number-binary-strings-without-consecut ...

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

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

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

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

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

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

  6. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

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

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

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

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

随机推荐

  1. JFinal WEB MVC和Struts简要对比

    JFinal遵循COC原则,零配置,无xml,而struts需要配置来支持action.result.interceptor配置与使用. JFinal开发效率非常之高,相对Struts开发效率能提升五 ...

  2. Geodetic集合

    图G是一个无向连通图,没有自环,并且两点之间至多只有一条边.我们定义顶点v,u最短路径就是从v到u经过边最少的路径.所有包含在v-u的最短路径上的顶点被称为v-u的Geodetic顶点,这些顶点的集合 ...

  3. github 如何添加项目代码

    1.点添加一个resporitory 2.添加的时候一定要选上下面的添加readme这个选项 3.点进去点code就能create file了.贴上代码就行.主要是第二步必须选对

  4. 外网不能访问阿里云服务器的apache服务

    今天弄了下Ubuntu的服务器,配置了一个LAMP环境的服务器,配置有时间我再来说 但是配置了很长的时间,差不都怕是好几个小时 在配置apache的时候,出现了问题,好不容易把apache服务配置好 ...

  5. vue webkit-box-orient: vertical打包线上不显示

    emmm……觉得不科学啊,写了几个vue的网站,限制超出行数省略号.结果发现放到线上,全都瓦特了.反复检查本地跑起来没错,代码没少,偏偏在线上的时候就是缺了vue -webkit-box-orient ...

  6. Django入门与实践-第11章:URL 分发(完结)

    http://127.0.0.1:8000http://127.0.0.1:8000/boards/1/http://127.0.0.1:8000/boards/2/http://127.0.0.1: ...

  7. KindEditor解决上传视频不能在手机端显示的问题

    KindEditor自带的上传视频生成的HTML代码为<embed>,在手机端并不支持.于是可以自己在控件里增加生成video标签相关代码. 参考https://www.jianshu.c ...

  8. 破解Excel密码

    https://zhidao.baidu.com/question/98055974.html 方法:1\打开文件2\工具---宏----录制新宏---输入名字如:aa3\停止录制(这样得到一个空宏) ...

  9. Mysql & Hive 导入导出数据

    ---王燕行转列sql select split(concat_ws(',',collect_set(cast(smzq as string))),',')[1] ,split(concat_ws(' ...

  10. (字符串处理)Fang Fang -- hdu -- 5455 (2015 ACM/ICPC Asia Regional Shenyang Online)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5455 Fang Fang Time Limit: 1500/1000 MS (Java/Others)  ...