357. Count Numbers with Unique Digits

来源:互联网 发布:linux svn 验证 编辑:程序博客网 时间:2024/05/17 02:34

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])


思路:

本题求一定范围内,数位上的数字不重复的个数

n为0时,只有一个0,n为1时有10个数字,n为2时有(9 * 9 + 10)=91个数字。。。。。。

public static int countNumbersWithUniqueDigits(int n) {if (n == 0)return 1;int res = 0;for (int i = 1; i <= n; i++) {res += count(i);}return res;}public static int count(int k) {if (k == 1)return 10;int res = 9;for (int i = 9; i >= (11 - k); i--) {res *= i;}return res;}


0 0
原创粉丝点击