diff --git a/README.md b/README.md index 5af4e239d..ff26f835d 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ LeetCode |376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | [C++](./algorithms/cpp/wiggleSubsequence/wiggleSubsequence.cpp)|Medium| |371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/description/) | [C++](./algorithms/cpp/sumOfTwoIntegers/SumOfTwoIntegers.cpp)|Easy| |367|[Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/description/) | [C++](./algorithms/cpp/validPerfectSquare/ValidPerfectSquare.cpp)|Easy| +|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/) | [C++](./algorithms/cpp/countNumbersWithUniqueDigits/CountNumbersWithUniqueDigits.cpp)|Medium| |350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [C++](./algorithms/cpp/intersectionOfTwoArrays/intersectionOfTwoArraysII.cpp)|Easy| |349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [C++](./algorithms/cpp/intersectionOfTwoArrays/intersectionOfTwoArrays.cpp)|Easy| |347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [C++](./algorithms/cpp/topKFrequentElements/topKFrequentElements.cpp)|Medium| diff --git a/algorithms/cpp/countNumbersWithUniqueDigits/CountNumbersWithUniqueDigits.cpp b/algorithms/cpp/countNumbersWithUniqueDigits/CountNumbersWithUniqueDigits.cpp new file mode 100644 index 000000000..6733dced3 --- /dev/null +++ b/algorithms/cpp/countNumbersWithUniqueDigits/CountNumbersWithUniqueDigits.cpp @@ -0,0 +1,70 @@ +// Source : https://leetcode.com/problems/count-numbers-with-unique-digits/ +// Author : Hao Chen +// Date : 2019-03-24 + +/***************************************************************************************************** + * + * 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 + * + ******************************************************************************************************/ + + +// Considering three digits +// - the first place could be [1-9] which has 9 choices. +// - the second place could be [0-9] with excluding the first digit, which is 10-1=9 choices. +// - the third place could be [0-9] with excluding the 1st and 2nd digits, which is 10-2=8 choices. +// So, three digits has 9*9*8 unique digits. +// +// After adds the 1 digit unique number,and 2 digits unique number, we can have the result: +// +// 9*9*8 + 9*9 + 10 = 648 + 81 + 10 = 739 +// +// n = 0, a[0] = 1; +// n = 1, a[1] = 9 + a[0]; +// n = 2, a[2] = 9*9 + a[1]; +// n = 3, a[3] = 9*9*8 + a[2]; +// n = 4, a[4] = 9*9*8*7 + a[3]; +// .... +class Solution { +public: + int countNumbersWithUniqueDigits(int n) { + int result = 1; + for (int i=0; i