C++ math solution, no DP, O(1) in time and space, 10 lines


  • 0
    int countNumbersWithUniqueDigits(int n) {
    	int numUniqueDigits = 1;
    	for (int i = 1; i <= n; i++) {
    		int numUniqueDigitsWithIDigits = 1;
    		for (int j = 0; j < i; j++)
    			numUniqueDigitsWithIDigits *= (j == 0 ? 9 : 10 - j);
    		numUniqueDigits += numUniqueDigitsWithIDigits;
    	}
    	return numUniqueDigits;
    }
    

Log in to reply
 

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