<p>Given a positive integer <code>n</code>, return the number of the integers in the range <code>[0, n]</code> whose binary representations <strong>do not</strong> contain consecutive ones.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong>
Here are the non-negative integers &lt;= 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. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
    string bit;
    int n;
    int dp[32][2][2];
public:
    int f(int i,int prev,int tight){
        if(i==(n))return 1;
        if(dp[i][prev][tight]!=-1)return dp[i][prev][tight];
        int cap=2;
        if(tight==1){
            cap=bit[i]-'0';
            // cout<<cap<<' '<<i<<' '<<prev<<endl;
        }
        int ans=0;
        for(int j=0;j<=1;j++){
            if(tight==1){
                if(j<cap){
                    if(prev==0 ||(j==1 && prev==0) || j==0){
                        ans+=f(i+1,j,0);
                    }
                }
                else if(j==cap){
                    if(prev==0 ||(j==1 && prev==0) || j==0){
                        ans+=f(i+1,j,1);
                    }
                    break;
                }
            }
            else{
                if(prev==0 ||(j==1 && prev==0) || j==0){
                    ans+=f(i+1,j,0);
                }

            }

        }
        return dp[i][prev][tight]=ans;
    }
    void toBits(int n){
        while(n>0){
            int b=n%2;
            n/=2;
            bit=to_string(b)+bit;
        }
    }
    int findIntegers(int n) {
        toBits(n);
        this->n=bit.size();
        // cout<<this->n<<endl;
        // cout<<bit<<endl;
        // if(this->n==1)return 1;
        memset(dp,-1,sizeof(dp));
        return f(0,0,1);


    }
};
11000