zoukankan      html  css  js  c++  java
  • 357. Count Numbers with Unique Digits

    问题:

    给定数字n,由n个数字(0~9)构成0~n位数,求构成的数中,不同数位上重复数字的数以外,有多少个数。

    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
    
    Constraints:
    0 <= n <= 8
    

    解法:Backtracking(回溯算法)

    对于本问题,两个变量:

    • 路径:已经选择好的前几位结果path
    • 选择列表:对该位置上元素的选择可能性:0~9中不在path中的任意数字

    处理过程:

    • base:递归退出条件:选择到最后一位结束,这里为已经选择好路径长度==给出的组合要求长度 n。
    • 做选择:对于当前位置,选择其中一个可用数字a。(不在path中的任意数字)
      • 路径.add(a)
      • 选择列表.delete(a):上一个选择数字 i + 1
    • 撤销选择:回退到选择数字a之前的状况。
      • 路径.delete(a)
      • 选择列表.add(a):i

    ⚠️  注意:特别的以0开头的数字,不要求开头的0与其他位的0进行重复。

    代码参考:

     1 class Solution {
     2 public:
     3     void dfs(int& res, int n, int pos, unordered_set<int>& path) {
     4         if(pos == n) {
     5             res++;
     6             return;
     7         }
     8         for(int i=0; i<10; i++) {
     9             if(path.count(i)==0) {
    10                 path.insert(i);
    11                 if(path.size()==1 && i==0) path.erase(i);
    12                 dfs(res, n, pos+1, path);
    13                 path.erase(i);
    14             }
    15         }
    16         return;
    17     }
    18     int countNumbersWithUniqueDigits(int n) {
    19         int res = 0;
    20         unordered_set<int> path;
    21         dfs(res, n, 0, path);
    22         return res;
    23     }
    24 };

    数学算法:

    参考leetcode discuss

    Following the hint. Let f(n) = count of number with unique digits of length n.

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

    f(2) = 9 * 9. Because for each number i from 1, ..., 9, we can pick j to form a 2-digit number ij and there are 9 numbers that are different from i for j to choose from.

    f(3) = f(2) * 8 = 9 * 9 * 8. Because for each number with unique digits of length 2, say ij, we can pick k to form a 3 digit number ijk and there are 8 numbers that are different from i and j for k to choose from.

    Similarly f(4) = f(3) * 7 = 9 * 9 * 8 * 7....

    ...

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

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

    any number with length > 10 couldn't be unique digits number.

    The problem is asking for numbers from 0 to 10^n. Hence return f(1) + f(2) + .. + f(n)

    代码参考:

     1 class Solution {
     2 public:
     3     int countNumbersWithUniqueDigits(int n) {
     4         int res = 10;
     5         if(n==0) return 1;
     6         int kcounts = 9;//k位数,能构成的符合条件的数字总数 
     7         int avalible = 9;//该位置余下可用的数字个数
     8         for(int i=n; i>1; i--) {
     9             kcounts = kcounts * avalible;
    10             res += kcounts;
    11             avalible--;
    12         }
    13         return res;
    14     }
    15 };
  • 相关阅读:
    常用命令
    经典算法
    框架
    计算机网络
    设计模式
    JVM
    数据库
    多线程
    Java中HashMap的底层实现原理
    构建大小顶堆
  • 原文地址:https://www.cnblogs.com/habibah-chang/p/14285510.html
Copyright © 2011-2022 走看看