Can you solve this real interview question? Count Numbers with Unique Digits - Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n. Example 1: Input: n = 2 Output: 91 Explanation: The answer should be the t
class Solution { public: int countNumbersWithUniqueDigits(int n) { if(n==0) return 1; int ans=0; for(int i=1;i<=n;i++) { ans+=fun(i); } return ans; } int fun(int n) { int m = 9; if(n==1) m=10; int m2=9; for(int i=0;i<n-1;i++) { m*=m2; m2--; ...
leetcode [357]Count Numbers with Unique Digits Given anon-negativeinteger n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Input:2Output:91Explanation:The answer should be the total numbers in the range of 0 ≤ x < 100, excluding11,22,33,44,55,66,77,88,99...
参考代码 packageleetcode// 暴力打表法funccountNumbersWithUniqueDigits1(nint)int{res:=[]int{1,10,91,739,5275,32491,168571,712891,2345851,5611771,8877691}ifn>=10{returnres[10]}returnres[n]}// 打表方法funccountNumbersWithUniqueDigits(nint)int{ifn==0{return1}res,uniqueDigits,availableNumber:=10...
public int countNumbersWithUniqueDigitsByLoop(int n) { if(n<=1) return (int)Math.pow(10, n); int count=0; for(long i=0;i<Math.pow(10, n);i++) { char[] array=(""+i).toCharArray(); Set<Character> set=new HashSet<>(); ...
leetcode [357]Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: 题目大意: 给一个数字n,找出[0,10^n)这个区间内,由不同数字组成的数字个数。 解法: 我采用递归的解法求解由不同数字组成的数字个数,flag...
classSolution {public:intcountNumbersWithUniqueDigits(intn) {if(n ==0)return1;intres =0;for(inti =1; i <= n; ++i) {res+=count(i); }returnres; }intcount(intk) {if(k <1)return0;if(k ==1)return10;intres =1;for(inti =9; i >= (11- k); --i) { ...
357. Count Numbers with Unique Digits Given anon-negativeinteger 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])...
Solution2 Code: classSolution{ publicintcountNumbersWithUniqueDigits(intn){ if(n ==0)return1; intres =10; intuniqueDigits =9; intavailableNumber =9; while(n-- >1&& availableNumber >0) { uniqueDigits = uniqueDigits * availableNumber; ...
[LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数 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 ...