码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-19 11:07:00      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:ber   enc   numbers   logs   from   this   repr   div   find   

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 }

 

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

标签:ber   enc   numbers   logs   from   this   repr   div   find   

原文地址:http://www.cnblogs.com/lz87/p/7288711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!