leetcode 600 Count number of binary strings without consecutive 1
来源:互联网 发布:王军 大数据 编辑:程序博客网 时间:2024/05/29 16:25
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s.
Examples:
Input: N = 2
Output: 3
// The 3 strings are 00, 01, 10
Input: N = 3
Output: 5
// The 5 strings are 000, 001, 010, 100, 101
思路是用DP,考虑从n位增加一位到n+1位的情况。如果首位是0,那么第n+1位的首位既可以是0,也可以是1,因为都不会产生连续的1;如果首位是1,那么只能生成首位是0的n+1位数,才能避免出现连续的1。
用zeros记录首
代码如下:
// C++ program to count all distinct binary strings// without two consecutive 1's#include <iostream>using namespace std;int countStrings(const int n){ vector<int> a(n), b(n); a[0] = b[0] = 1; for (int i = 1; i < n; i++) { a[i] = a[i - 1] + b[i - 1]; b[i] = a[i - 1]; } return a[n - 1] + b[n - 1];}// Driver program to test above functionsint main(){ cout << countStrings(3) << endl; return 0;}
阅读全文
0 0
- leetcode 600 Count number of binary strings without consecutive 1
- geeksforgeeks Count number of binary strings without consecutive 1’s
- geeksforgeeks Count number of binary strings without consecutive 1′s
- Distinct binary strings of length n with no consecutive 1s
- count the number of "1" in the binary figure
- python int to binary and count the number of 1
- leetcode:count the number of nodes
- Leetcode 600 Non-negative Integers without Consecutive Ones
- Count the number of 1(MIT hackmem)
- Count Binary Substrings(leetcode)
- function to count the number of "1" of one byte
- 【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
- LeetCode:Number of 1 Bits
- LeetCode-Number of 1 Bits
- leetcode: Number of 1 Bits
- LeetCode Number of 1 Bits
- Leetcode: Number of 1 Bits
- Leetcode--Number of 1 Bits
- Yii2连接oracle中文乱码解决方法之一
- abstract class和interface的区别
- 如何通过各种数据挖掘运维价值
- 网络流24题一句话题解集合
- mongodb笔记03(MongoDB 条件操作符)
- leetcode 600 Count number of binary strings without consecutive 1
- 三大操作系统比较,为什么开发要用Linux系统?
- php怎样判断一个字符串是否包含另一个字符串?
- Windows10 下安装 py-faster-rcnn
- css+div横向滚动条
- 设计模式<四>简单工厂模式与工厂方法模式
- 除了获奖感言 吴恩达们说人工智能的2018要这么走! | AI英雄风云榜
- 【目标检测大集合】R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记
- 细思极恐 如果人工智能真的形成自我意识会发生什么? | 研究