Java recursion


  • 0
    I
    public class Solution {
        public int countNumbersWithUniqueDigits(int n) {
            if(n == 0) return 1;
            else if(n == 1) return 10;
            else return 10 + 9*count(Math.min(n-1, 9), 9);
        }
        
        private int count(final int depth, final int usable) {
            if(depth == 0 || usable == 0) return 0;
            
            //x, usable 3
            //x1, x2, x3
            //x1 * (2,3)
            //x2 * (1,3)
            //x3 * (1,2)
            int ans = usable + usable * count(depth-1, usable - 1);
            
            return ans;
        }
    }```

Log in to reply
 

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