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 ...
随机推荐
- mysql批量数据导入探究
最近工作碰到一个问题,如何将大量数据(100MB+)导入到远程的mysql server上. 尝试1: Statement执行executeBatch的方法.每次导入1000条记录.时间为12s/10 ...
- Ubuntu下部分软件的简介及安装
1.安装linux摄像头应用软件cheese sudo apt-get install cheese 2.Ubuntu Tweak Ubuntu Tweak是一款专门为Ubuntu(GNOME桌 ...
- dedecms的if标签、foreach标签
1.if标签 (1)下拉列表 <select name="prize_type[]" class="type J-prize-type" id=" ...
- jsp札记
日期格式化 <s:date name="creaetime" format="yyyy-MM-dd HH:mm:ss" /> <base hr ...
- 2018.09.17 atcoder Tak and Cards(背包)
传送门 背包经典题. 直接f[i][j]f[i][j]f[i][j]表示选i张牌和为j的方案数. 最后统计答案就行了. 代码: #include<bits/stdc++.h> #defin ...
- 2018.08.19 NOIP模拟 change(简单模拟)
Change 题目背景 SOURCE:NOIP2015-SHY-10 题目描述 Alice 和 Bob 又聚在一起了!他们已经厌倦了取石子游戏,现在他们热衷于切题.于是,Alice 找到了一道题让 B ...
- 2018.07.26NOIP模拟 魔法数字(数位dp)
魔法数字 题目背景 ASDFZ-NOIP2016模拟 题目描述 在数论领域中,人们研究的基础莫过于数字的整除关系.一般情况下,我们说整除总在两个数字间进行,例如 a | b(a能整除b)表示 b 除以 ...
- Nginx中间件使用心得(二)
一.基础知识补充 1. 历史由来补充: Nginx是俄罗斯第二大网站的开源项目. 淘宝团队发行了 tengine 增加了很多第三方的包. 2.下载相关的主键 (1)nginx下载地址 ...
- Redis配置总结
一:常用配置 1.bind,格式为bind 127.0.0.1:这个是很重要的配置,如果bind 127.0.0.1则外部网络是访问不了的(如果外部网络要访问还要开放端口) 2.port,格式为por ...
- b4和tncl_extract_UNCL_new
# -*- coding:utf-8 -*- import re ''' 适应新版本 注意: 1)17A文件改完后缀后,需要转为UTF-8无BOM格式,才能正确处理. 2)fr = open(file ...