nonnegative integer i am having tle


  • 0
    S

    hey i am having a time limit exceed please help me to fix this i am using iterative way to do this but having some bugs...

    class Solution {
    public:
    
    int bin(int n)
    {
    unsigned i;
    long unsigned j;
        int flag;
    
        int out;
        long int c=0;
    
        for(j=0;j<=n;j++)
        {
            flag=0;
            out=0;
    
        for (i=1<<31 ; i > 0; i = i / 2)
        {
            if( (j & i) && (flag==0) )
                {//printf("1");
                    flag=1;
                }
            else if((j & i) && (flag==1))
                {//printf("1");
                out=1;
                break;}
    
            else
                //printf("0");
                flag=0;
    
        }
    
    
        if((flag==0 || flag ==1) && (out!=1))
        {
            c++;
            //printf("value of %d",c);
            flag=0;
           // printf("\n");
        }
    else{
        //printf("\n");
        continue;
    }
    
        }
    return c;
    }
    
        int findIntegers(int num) {
       return bin(num);
        }
    };
    

  • 0
    C

    If you loop through all number smaller than n and check if they have consecutive 1 one by one, you will get TLE. DP is the correct method.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.