[Coding Made Simple] Number without consecutive 1s in binary representation

Given a number n, find the total number of numbers from 0 to 2^n - 1 which do not have consecutive 1s in their binary representation.

Solution.  This problem is the equivalence of fibonacci sequence. For a given n, f(n) = f(n - 1) + f(n - 2).

 1 public int getTotalNumberOfNoConsecutiveOnes(int n) {
 2     if(n == 0) {
 3         return 1;
 4     }
 5     if(n == 1) {
 6         return 2;
 7     }
 8     int[] T = new int[n + 1];
 9     T[0] = 1;
10     T[1] = 2;
11     for(int i = 2; i <= n; i++) {
12         T[i] = T[i - 1] + T[i - 2];
13     }
14     return T[n];
15 }
时间: 2024-12-16 03:23:02

[Coding Made Simple] Number without consecutive 1s in binary representation的相关文章

[LeetCode] Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin

762. Prime Number of Set Bits in Binary Representation 二进制表示形式中的素数位数

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin

Leetcode 762. Prime Number of Set Bits in Binary Representation

思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). 1 class Solution { 2 public int countPrimeSetBits(int L, int R) { 3 Set<Integer> prime = new HashSet<Integer>(Arrays.asList(2,3,5,7,11,13,17,19,23)); 4 int[] count = new int[1+R]; 5 int res = 0; 6 for

[Coding Made Simple] Maximum Subsquare surrounded by &#39;X&#39;

Given a 2D matrix where every element is either 'O' or 'X', find the largest subsquare surrounded by 'X'. Examples: Input: mat[N][N] = { {'X', 'O', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'X', 'O'}, {'X', 'X', 'X', 'X', 'X'}, {'X',

[Coding Made Simple] Box Stacking

Given boxes of different dimensions, stack them on top of each other to get maximum height such that box on top has strictly less length and width than box under it. Algorithm. 1. get all the box permutation with each permutation's length >= width; s

[CareerCup] 5.2 Binary Representation of Real Number 实数的二进制表示

5.2 Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR."

[Coding Made Simple] Count Number of Binary Tree Possible given Preorder Sequence

Count Number of Binary Tree Possible given Preorder Sequence. For example, given preorder sequence of {10, 11, 9, 12, 13, 14}, the total possible number of binary trees is 42. Solution 1. Recursion Since we are only trying to find possbile binary tre

[Coding Made Simple] Coin Changes Number of ways to get a total

Given coins of certain denominations and a total, how many ways these coins can be combined to get the total. Dynamic Programming solution State: T[i][j]: given the first i coins, the total number of ways these coins can be combined to get the total

[Coding Made Simple] Coin Changes Minimum Number of Coins

Given coins of certain denominations and a total, how many minimum coins would you need to make this total? Dynamic Programming solution State: T[i][j]: given the first i coins, the min number of coins needed to make a total of j. Function: case 1. T