Java based on Math and backtracking


  • 0
    J
    public int countNumbersWithUniqueDigits(int n) {
    	if (n == 0)
    		return 1;
    	int res = 0;
    	return helper(res, n);
    }
    private int helper(int res, int n) {
    	if (n == 1)
    		return res + 10;    / /one digit
    	int result = 1;
    	for (int i = 11 - n; i <= 9; i++)
    		result *= i;
    	result *= 9;
    	return helper(res + result, --n);
    }
    

Log in to reply
 

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