Number of Digit One:runtime 0ms


  • 0
    H
    class Solution {
    public:
        int countDigitOne(int n) {
         if(n<10)
    	   {
    			if(n>=1)
    				return 1;
    			else
    				return 0;
    	   }
    	   //
    	   int base=1,len=0;
    	   int tmp=n;
    	   while(tmp=tmp/10)
    	   {
    			base*=10;
    			len++;
    	   }
    	   if((n/base)>1)
    			return base+countDigitOne(n%base)+(n/base)*digitOne(len);
    	   else
    		   return n-base+countDigitOne(n%base)+digitOne(len)+1;
        }
          int digitOne(int n)
       {
    		int tmp=n-1,count=1;
    		while(tmp)
    		{
    			count*=10;
    			--tmp;
    		}
    	   return count*n;
       }
    };

Log in to reply
 

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