编程练习(第十七周)
来源:互联网 发布:sql语句实现分页查询 编辑:程序博客网 时间:2024/05/22 09:07
题目来源:https://leetcode.com
600. Non-negative Integers without Consecutive Ones
DescriptionHintsSubmissionsSolutions
- Total Accepted: 1587
- Total Submissions: 6069
- Difficulty: Hard
- Contributors:sanxi
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: 5Output: 5Explanation: Here are the non-negative integers <= 5 with their corresponding binary representations:0 : 01 : 12 : 103 : 114 : 1005 : 101Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
Note: 1 <= n <= 109
代码如下:
int findIntegers(int num) { string str=""; while(num>0)str+=num%2+'0',num/=2; int l=str.length(); vector<int> arr(l+1,1); for(int i=2;i<=l;i++)arr[i]=arr[i-1]+arr[i-2]; int result=1,pre=0; for(int i=l;i>=0;i--) { if(str[i-1]=='1') { result+=arr[i]; if(pre==1)break; } pre=str[i-1]=='1'; } return result-pre;}
阅读全文
0 0
- 编程练习(第十七周)
- C++primer plus第十七章编程练习
- C primer plus(第五版)编程练习第十七章
- Android编程权威指南-第十七章挑战练习
- C ++ Primer Plus 第六版 第十七章编程练习答案
- 《C++ Primer Plus(第六版)》(41)(第十七章 输入、输出和文件 编程练习和答案1)
- 《C++ Primer Plus(第六版)》(42)(第十七章 输入、输出和文件 编程练习和答案2)
- 第十七周自由练习项目——acm 商品 销售
- 第十七周项目4-日期体结构 结构体练习
- 第十七周(ZigZag Conversion)
- C++ Primer Plus第六版 第十七章 编程练习答案(完结篇...木有18章)
- 编程练习(第二周)
- 编程练习(第三周)
- 编程练习(第五周)
- 编程练习(第八周)
- 编程练习(第七周)
- 编程练习(第九周)
- 编程练习(第十二周)
- 关于谓词函数predicates的介绍
- DPM算法
- C++STL之string
- C++ zip文件的解压与压缩
- for循环中pairs与ipairs的区别与联系
- 编程练习(第十七周)
- C# 两个独立exe程序直接通信
- .NET Expression 从零开始四:结构化异常处理(SEH)
- 计算网页停留时间的平均时长
- 霍尼韦尔C300-DCS系统学习课件
- C6678的中断控制器
- Coin Change
- 基于安卓6.0开机启动动画到Launcher的流程
- Android自定义View,你必须知道的几点