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 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的更多相关文章
- Non-negative Integers without Consecutive Ones
n位二进制,求不包含连续1的二进制(n位)数字个数. http://www.geeksforgeeks.org/count-number-binary-strings-without-consecut ...
- 第十六周 Leetcode 600. Non-negative Integers without Consecutive Ones(HARD) 计数dp
Leetcode600 很简单的一道计数题 给定整数n 求不大于n的正整数中 二进制表示没有连续的1的数字个数 在dp过程中只要保证不出现连续1以及大于n的情况即可. 所以设计按位dp[i][j]表示 ...
- [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 ...
- [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 ...
- [Algorithm] Count Negative Integers in Row/Column-Wise Sorted Matrix
// Code goes here function countNegative (M, n, m) { count = ; i = ; j = m - ; && i < n) ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...
- [LintCode] Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...
- Lintcode: Interleaving Positive and Negative Numbers 解题报告
Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-pos ...
随机推荐
- iOS开发总结
最近在工作中使用Objective-C开发iOS客户端程序,它一方面和Server通讯,处理网络连接,收发报文,实现业务逻辑;另一方面为UI层提供各种业务API. 下面记录用到的相关知识点,困难, ...
- 如何移除 input type="number" 时浏览器自带的上下箭头?
Chrome 下 input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: no ...
- eclipse中导入dtd文件实现xml的自动提示功能
以mybatis为例 1.mybatis的xml文件头: (1)config文件: <?xml version="1.0" encoding="UTF-8" ...
- /etc/inittab加入自动启动格式
R01:35:respawn:/usr/bin/exe_program 说明 R01:标识,每一行必须唯一(R01并无特殊含义,可自定义). 35:有效模式,3字符界面启动,5图形界面启动 respa ...
- 2018.07.24 bzoj3531: [Sdoi2014]旅行(树链剖分+动态开点)
传送门 树链剖分. 如何维护? 如果颜色少直接每种颜色一颗线段树走人. 但这题颜色数量不大于1e5" role="presentation" style="po ...
- 马婕 2014MBA专硕考试 报刊选读 4 朝鲜战争会爆发吗?(转)
http://blog.sina.com.cn/s/blog_3e66af4601016ela.html War unlikely, but Koreans still on cliff edge 战 ...
- 第四章 代词(Les pronoms )
★人称代词 .主语人称代词 第一人称和第二人称属纯人称代词,只能代人不能代物;第三人称可代人,亦可代物.如: La Terre est ronde. Elle tourne autour du Sol ...
- Django入门与实践-第21章:迁移(完结)
http://127.0.0.1:8000/boards/1/ python manage.py migrate #boards/models.py class Topic(models.Model) ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- python5-常用模块
collection 模块 # pypi 可以查询python的模块在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counte ...