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. AE教程:学会这个,你做的Logo就可以单独出道了

    一.确定所需要做的动效 1.制作logo背景形状动效 2.制作U的动效 3.制作I的动效 4.制作消失动效 二.制作logo背景形状动效1.”合成 - 新建合成“ 新建一个1000*800的合成 2. ...

  2. 02 Maven 入门使用

    Maven 入门使用 1. Maven 项目工程目录约定 Project |-src | |-main | | |-java -- 存放项目的 .java 文件 | | |-resources -- ...

  3. 用Notepad++在文本文件里快速在每行头尾都加上指定的内容(转载)

  4. 一个 图片 滚动 飞入的css特效

    @keyframes bounceInLeft { from, 60%, 75%, 90%, to {animation-timing-function: cubic-bezier(0.215, 0. ...

  5. Axios的基本使用

    Axios的基本使用 介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.在vue 中,用来发ajax请求,与后端交互. 从浏览器中创建 XMLHtt ...

  6. Linux服务器部署系列之七—OpenLDAP篇

    LDAP(轻量级目录访问服务),通过配置这个服务,我们也可以在linux下面使用目录的形式管理用户,就像windows下面的AD一样,方便我们管理.下面我们就一起来配置openldap服务.本文运行环 ...

  7. Spring Boot的自动配置的原理浅析

    一.原理描述 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Spring容器. 二. ...

  8. LA 4329 Ping pong (树状数组)

    题意:从左到右给你n个不同的数值,让你找出三个数值满足中间的数值在两边的数值之间的个数. 析:题意还是比较好理解的,关键是怎么求数量,首先我们分解一下只有两种情况,一个是左边<中间<右边, ...

  9. Android的方法数超过65535问题

    Under the Hood: Dalvik patch for Facebook for Android 先来看一段中文内容 Hack Dalvik VM解决Android 2.3 DEX/Line ...

  10. 几个经典的数学库之一学习---VCGlib(3)

    Camera and shot abstraction for managing views 视图的定义,以及mesh的操作说明. Shot(镜头) and camera(相机) shot摄像结构以及 ...