本文主要是介绍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]
)
Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.
题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/
public class Solution {public int countNumbersWithUniqueDigits(int n) {if (n == 0) {return 1;}int ans = 10, cur = 9;for (int i = 2; i <= Math.min(n, 10); i ++) {cur = cur * (11 - i);ans += cur;}return ans;}
}
这篇关于LeetCode 357 Count Numbers with Unique Digits (排列组合)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!