Count Numbers with Unique Digits

解题思路一:

找规律,动态规划。

f(n):长度为n的数字中包含的独立数位的数字个数。

f(1) = 10 (0,1,2,3,4,...9)

f(2)=9*9  ,因为十位只能是(1,2,...9),对于十位的每个数,个位只能取其余的9个数字。

f(3)=f(2)*8=9*9*8

f(10)=9*9*8*7...*1

f(11)=0=f(12)=f(13)....

class Solution {
public:
    int countNumbersWithUniqueDigits(int n) {
         if(n==0)return 1;
         int res=10,available=9,cur=9;
         for(int i=1;i<n;i++)
         {
         	  if(cur==0)break;
              int tmp = available*cur;
              res+=tmp;
              available=tmp;
              cur--;

         }
         return res;
    }
};

解法二:回溯法

class Solution {
public:
   //遍历所有的数字,includeZero用来记录是否该数位使用0.
    void compute(int n,int& result,vector<bool>& used,bool includeZero)
    {
         ++result; //统计次数
         if(n==0)return;
         for(int i=includeZero?0:1;i<=9;++i)
         {
             if(used[i])continue;
             used[i]=true;
             compute(n-1,result,used,true);
             used[i]=false;

         }

    }

    int countNumbersWithUniqueDigits(int n) {

         vector<bool> used(10,false); //标示数组,记录0-9每个数位是否被使用
         int result = 0;
         compute(n,result,used,false);
         return result;

    }
};
时间: 2024-12-16 22:26:38

Count Numbers with Unique Digits的相关文章

leetCode 357. Count Numbers with Unique Digits | Dynamic Programming | Medium

357. 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 [11,22,3

Java [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: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]) 解题思路: This

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 [11,22,33,44,55,66,77,88,99]) Hint: 1.A d

[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 [11,22,33,44,55,66,77,88,99]) Hint: A direct

【leetcode】357. Count Numbers with Unique Digits

题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. 解题分析: 题目要就就是找出 0≤ x < 10n中各位数字都不相同的数的个数.要接触这道题只需要理解: 1.设f(n)表示n为数字中各位都不相同的个数,则有countNumbersWithUniqueDigits(n)=f(1)+f(2)+……+f(n)=f(n)+countNumbersWithU

357. 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 [11,22,33,44,55,66,77,88,99]) 思路: 这道题要求0 ≤

【Leetcode】Count Numbers with Unique Digits

题目链接:https://leetcode.com/problems/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

Count Numbers with Unique Digits -- LeetCode

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n. 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]) 思路:DP dp[i]表示10

357. 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 [11,22,33,44,55,66,77,88,99]) Credits:Special

[Swift]LeetCode357. 计算各个位数不同的数字个数 | Count Numbers with Unique Digits

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Input: 2 Output: 91 Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100,   excluding 11,22,33,44,55,66,77,88,99 给定一个非负整数