Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
代码如下:
1 public class Solution { 2 public int countNumbersWithUniqueDigits(int n) { 3 if(n==0) 4 return 1; 5 int sum=0,p=0; 6 7 if(n<=2) 8 return (int)Math.pow(9,n)+countNumbersWithUniqueDigits(n-1); 9 else{ 10 sum=countNumbersWithUniqueDigits(n-1); 11 n=n-2; 12 p=81; 13 int q=8; 14 while(n>0) 15 { 16 p=p*q; 17 q--; 18 n--; 19 } 20 21 } 22 return sum+p; 23 } 24 }
浙公网安备 33010602011771号