leetcode| Count Numbers with Unique Digits

Source: Internet
Author: User

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 is the total numbers in the range of 0≤x <, excluding [11,22,33,44,55,66,77,88,99] )

Title: The meaning is very clear, find out 0 to 10 of the n the number of elements that do not have duplicate numbers, 11,101 of these are unqualified

Idea: This topic is very representative, the method of violence is certainly possible, but according to the tip of the topic label, you can use backtrack and dynamic programming, this two algorithm is worth in-depth study, see many books mentioned

1, the first is backtrack, the scientific name backtracking algorithm, the basic idea is: from a road forward, can enter into, can not enter back, change a road and try again.

The idea of the following solution is, for a given number of range 0≤x < 10^n, we first in the interval to seek 1, followed by 2. To 9 the number of rules that match the rule, 1 starts inside

We beg 11,12,13 .... 19 The beginning, so recursive, sets a Boolean array to manage which number should be circumvented, and the number that should be circumvented is set to True

public int countnumberswithuniquedigits (int n) {
if (n > 10) {
Return Countnumberswithuniquedigits (10);
}
int count = 1; x = = 0
Long max = (long) Math.pow (ten, N);

boolean[] used = new BOOLEAN[10];

for (int i = 1; i < i++) {
Used[i] = true;
Count + = Search (i, max, used);
Used[i] = false;
}

return count;
}

private static int search (long prev, long Max, boolean[] used) {//query prev, RANGE Prev-max number
int count = 0;//Initial
if (prev < max) {//Recursive exit
Count + = 1;
} else {
return count;
}

/**
* This logic means, suppose prev = 1,i=2, at this time used array used[1] is true,used[2] is True,curr = 12, then go to query 12, to max number of
*/
for (int i = 0; i < i++) {
if (!used[i]) {
Used[i] = true;
Long cur = ten * prev + i;
Count + = search (cur, max, used);
Used[i] = false;
}
}

return count;
}

2, this kind of thinking is relatively simple to understand a little, the principle is to transform the multi-stage process into a series of single-stage problems, the use of the relationship between the phases, one by one, to create a solution to such process optimization problems of the new method

In this case, we can find that a rule where we use F (n) to indicate the number of conforming elements of an n-digit number, such as f (2), identifies 10-99 intervals.

Will find that f (n) conforms to such a law, f (n) = 9*9*8 ... (11-n), so there is the following code

public int countnumberswithuniquedigits (int n) {
if (n==0) return 1;
int count = 10;
int digit = 9;
for (int i = 2;i<=n;i++) {
Digit *= (11-i);
Count+=digit;
}
return count;
}

Leetcode| Count Numbers with Unique Digits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.