Non-negative Integers without Consecutive Ones
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
题目:
给定的数字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的更多相关文章
- [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 ...
- 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 ...
- [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 600. Non-negative Integers without Consecutive Ones(HARD) 计数dp
Leetcode600 很简单的一道计数题 给定整数n 求不大于n的正整数中 二进制表示没有连续的1的数字个数 在dp过程中只要保证不出现连续1以及大于n的情况即可. 所以设计按位dp[i][j]表示 ...
- 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 ...
- SH Script Grammar
http://linux.about.com/library/cmd/blcmdl1_sh.htm http://pubs.opengroup.org/onlinepubs/9699919799/ut ...
随机推荐
- 二维码API接口
1.http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://www.54admin.net 2.http://b.bshare ...
- Number的Util
1. NumberUtils.isNumber() : 判断字符串是否是数字 NumberUtils.isNumber("5.96");//结果是true NumberUtils. ...
- [福大2018高级软工教学]团队Alpha阶段成绩汇总
一.作业地址: https://edu.cnblogs.com/campus/fzu/AdvancedSoftwareEngineerning2018/homework/2396 https://ed ...
- week05 06绑定滚动条 去抖动
像这种小代码 为了满足某种需求 可以直接上网搜 这些都是JS代码和react无关 我们下拉 就会触发事件从而调用loading more news 那个函数 react要求我们加个key key就是唯 ...
- Mybatis之mapper.xml配置文件中的#{}和${}
#{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换.#{}可以有效防止sql注入. #{}可以接收简单类型值或pojo ...
- 安装 protoc 的各种坑
首先下载 protoc 2.6.1 https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.g ...
- java-学习1
作为一个想要深入的程序猿,只是学习前端是不够的,我总结我的前端工作是围绕着html.css.js展开写的再好也是展现在表面,所以 我想学习一门能够深入的后台语言,想来想去我还是选择java作为以后深入 ...
- vue set
Vue 可以通过数组变异的方法可以控制数组的增减,却不能更改数组及对象,vue可以通过set方法改变数组的长度,改变某项的值,在组件中可以使用$set方法改变数组长度和某项的值 调用方法:Vue.se ...
- clone()与image和 cloneTo()
Mat image = imread("1.png" ) ; Mat image1 ; Mat image1(image) ;//仅是创建了Mat的头部分,image1与image ...
- linux服务器设置只允许密钥登陆
首先需要修改一些配置文件 vim /etc/ssh/sshd_config 进入sshd_config文件后需要更改几个地方 PubkeyAuthentication yes #启用公告密钥配对认证方 ...