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.

Subscribe to see which companies asked this question

static int mm[10];
static int m;
class Solution {
public:

    int countNumbersWithUniqueDigits(int n) {
        int count=1;
        if(n==0) return count;
        m=pow(10,n);
        int sum=0;
        for(int i=1;i<10;++i)
        {
            sum=i;
            mm[i]=1;
            count++;
            backtrack(sum,count);
            mm[i]=0;
        }
        return count;
    }
    void backtrack(int sum,int& count)
    {
        
            for(int i=0;i<10;++i)
            {
                int temp=sum*10+i;
                if(temp

你可能感兴趣的:(Leetcode,算法,easy)