Files
2020-08-07 17:06:53 +08:00

29 lines
1.6 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [357. 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:**
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
## 题目大意
给定一个非负整数 n计算各位数字都不同的数字 x 的个数其中 0 x < 10^n
## 解题思路
- 输出 n 位数中不出现重复数字的数字的个数
- 这道题摸清楚规律以后可以直接写出最终所有答案答案只有 11
- 考虑不重复数字是如生成的如果只是一位数不存在重复的数字结果是 10 如果是二位数第一位一定不能取 0那么第一位有 1-99种取法第二位为了和第一位不重复只能有 0-910种取法中减去第一位取的数字那么也是 9 种取法以此类推如果是三位数第三位是 8 种取法四位数第四位是 7 种取法五位数第五位是 6 种取法六位数第六位是 5 种取法七位数第七位是 4 种取法八位数第八位是 3 种取法九位数第九位是 2 种取法十位数第十位是 1 种取法十一位数第十一位是 0 种取法十二位数第十二位是 0 种取法那么第 11 位数以后每个数都是重复数字的数字知道这个规律以后可以累积上面的结果把结果直接存在数组里面暴力打表即可O(1) 的时间复杂度