Java 0ms solution - using math


  • 0
    H
    public class Solution {
        public int countNumbersWithUniqueDigits(int n) {
            if(n == 0) return 0;
            if(n == 1) return 10;
            if(n > 10) return countNumbersWithUniqueDigits(10);
            
            int k = n, sum = 9;
     
            while(k > 1){
                sum *= (11 - k);
                k--;
            }
            
            return sum + countNumbersWithUniqueDigits(n-1);
            
        }
    }

Log in to reply
 

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