LeetcodeCount Numbers with Unique Digits(计算各个位数不同的数字个数)

Posted sunbines

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetcodeCount Numbers with Unique Digits(计算各个位数不同的数字个数)相关的知识,希望对你有一定的参考价值。

357. Count Numbers with Unique Digits(计算各个位数不同的数字个数)

题目:链接

 1 class Solution {
 2 private:
 3     vector<vector<int>> rems;
 4 public:
 5     int countNumbersWithUniqueDigits(int n)
 6     {
 7         int sums = 0;
 8         if(n == 0)
 9             return 1;
10         vector<int> out;
11         for (int i = 1; i <= n; ++i)
12             backtracking(i, 0, out, sums);
13         return sums;
14     }
15 
16     void backtracking(int n, int end, vector<int> &out, int &sums)
17     {
18         if (end == n)
19         {
20             sums++;
21             return;
22         }
23         if (end == 0 && n != 1)
24         {
25             for (int j = 1; j <= 9; ++j)
26                 if (find(out.begin(), out.end(), j) != out.end())
27                     continue;
28                 else
29                 {
30                     out.push_back(j);
31                     backtracking(n, end + 1, out, sums);
32                     out.pop_back();
33                 }
34         }
35         else
36         {
37             for (int j = 0; j <= 9; ++j)
38                 if (find(out.begin(), out.end(), j) != out.end())
39                     continue;
40                 else
41                 {
42                     out.push_back(j);
43                     backtracking(n, end + 1, out, sums);
44                     out.pop_back();
45                 }
46         }        
47     }
48 };

 

以上是关于LeetcodeCount Numbers with Unique Digits(计算各个位数不同的数字个数)的主要内容,如果未能解决你的问题,请参考以下文章

LeetcodeCount of Smaller Numbers After Self

LeetcodeCount of Smaller Numbers After Self

LeetcodeCount Numbers with Unique Digits(计算各个位数不同的数字个数)

LeetcodeCount Primes

LeetcodeCount and Say

来自java的wit.ai消息api调用