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 thanks to @memoryless for adding this problem and creating all test cases.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

class Solution:

    def countNumbersWithUniqueDigits(self, n):

        """

        :type n: int

        :rtype: int

        """

        used = [False for x in range(10)]

        def gen(used, n, d):

            if n == d:

                return 1

            total = 1

            startIndex = 1 if d == 0 else 0

            for i in range(startIndex, 10):

                if not used[i]:

                    used[i] = True

                    total += gen(used, n, d + 1)

                    used[i] = False

            return total

        return gen(used, n, 0)

s = Solution()

a = s.countNumbersWithUniqueDigits(7)

print(a)

class Solution:
    def countNumbersWithUniqueDigits(self, n):
        """
        :type n: int
        :rtype: int
        """
        used = [False for x in range(10)]

        def gen(used, n, d):
            if n == d:
                return 1
            total = 1
            startIndex = 1 if d == 0 else 0
            for i in range(startIndex, 10):
                if not used[i]:
                    used[i] = True
                    total += gen(used, n, d + 1)
                    used[i] = False
            return total

        return gen(used, n, 0)

s = Solution()
a = s.countNumbersWithUniqueDigits(7)
print(a)

Solution

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8445742.html

时间: 2024-08-05 07:15:10

357. 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】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 ≤

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

给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n.示例:给定 n = 2,返回 91.(答案应该是除[11,22,33,44,55,66,77,88,99]外,0 ≤ x < 100 间的所有数字) 详见:https://leetcode.com/problems/count-numbers-with-unique-digits/description/ C++: class Solution { public: int countNumbersWith

LeetCode 357 Count Numbers with Unique Digits

题意: 给定一个非负数n,要求算出所有符合条件的x.要求 0 ≤ x < 10^n,并且x的各位都不相同. 题解: 当n=0时,0<=x<1,因此只有x=0一种情况. 当n=1时,0<=x<10,即0-9共10种情况. 当n>=2时,最高位为1-9共9种情况,第二位不能与上一位相同但可以为0,因此也是9种情况,下一位是8种情况以此类推. 应此结果是f(1)+f(2)+f(3)+....+f(n) 而f(k) = 9 * 9 * 8 * ... (9 - k + 2) (

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】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