1. 程式人生 > >【Leetcode】Count Numbers with Unique Digits(計算各個位數不同的數字個數)

【Leetcode】Count Numbers with Unique Digits(計算各個位數不同的數字個數)

microsoft code nbsp ktr https 個數 col uniq ble

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 };

【Leetcode】Count Numbers with Unique Digits(計算各個位數不同的數字個數)