Unique n = Unique n-1 + unique n digits, Java with pattern in comments


  • 0
    A

    I think the comments and variable name are self explanatory.

    public class Solution {
        public int countNumbersWithUniqueDigits(int n) {
            //u_0 = 1
            //u_1 = u_0 + 9 unique with 1 digits | d_1
            //u_2 = u_1 + 9*9 unique with 2 digits | d_2
            //u_3 = u_2 + 9*9*8 unique with 3 digits | d_3
            int u_i = 1;
            for(int i=1;i<=n;i++){
                int d_i = 9;
                for(int j=9;j>10-i;j--) 
                    d_i = d_i*j;
                u_i = u_i + d_i;
            }
            return u_i;
        }
    }
    

Log in to reply
 

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