Share my O(n) java solution


  • 0
    Z
    public class Solution {
    public int countNumbersWithUniqueDigits(int n) {
        if(n<0){
            return 0; 
        }
        if(n == 0){
            return 1;
        }
        int sum = 10;
        int cnt = 1;
        int num = 9;
        while(cnt < n){
            num *= (9 -cnt +1);
            sum += num;
            cnt ++;
        }
        return sum;
    }
    

    }


Log in to reply
 

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