【LeetCode】357. Count Numbers with Unique Digits

来源:互联网 发布:mac格式化u盘 编辑:程序博客网 时间:2024/06/07 06:57

问题描述

问题链接:https://leetcode.com/problems/count-numbers-with-unique-digits/#/description

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

Hint:

A direct way is to use the backtracking approach.

我的代码

因为是BackTracking的专项练习,所以就不需要什么思路了。主要碰到的问题是如何判定是否包含重复的数字,有个前导0的问题需要处理,把这个歌解决掉以后就通过了。

public class Solution {    private int count = 0;    public int countNumbersWithUniqueDigits(int n) {        /*        思路是使用BackTracking,如果前面没有重复的数字就可以继续        */        int[] numArr = new int[n];        for(int i = 0; i < n; i++){            numArr[i] = -1;        }        helper(numArr, 0, n);        return count;    }    private void helper(int[] numArr, int startPos, int N){        if(startPos == N){            // 得到一个解            count++;            return;        }        for(int j = 0; j < 10; j++){            numArr[startPos] = j;            if(unique(numArr, startPos)){                helper(numArr, startPos + 1, N);            }            // 感觉这里没法清理啊,先写上            numArr[startPos] = 0;        }    }    private boolean unique(int[] numArr, int stopPos){        // 不处理前导0        int startPos = 0;        int i = 0;        for(; i <= stopPos; i++){            if(numArr[i] != 0){                break;            }        }        for(; i <= stopPos; i++){            for(int j = i; j <= stopPos; j++){                if((i != j) && (numArr[i] == numArr[j])){                    return false;                }            }        }        return true;    }}

打败了0.16%的Java代码。还差的太远了,到讨论区学习一下。

讨论区

JAVA DP O(1) solution.

链接地址:https://discuss.leetcode.com/topic/47983/java-dp-o-1-solution

想法很好。

Following the hint. Let f(n) = count of number with unique digits of length n.

f(1) = 10. (0, 1, 2, 3, …., 9)

f(2) = 9 * 9. Because for each number i from 1, …, 9, we can pick j to form a 2-digit number ij and there are 9 numbers that are different from i for j to choose from.

f(3) = f(2) * 8 = 9 * 9 * 8. Because for each number with unique digits of length 2, say ij, we can pick k to form a 3 digit number ijk and there are 8 numbers that are different from i and j for k to choose from.

Similarly f(4) = f(3) * 7 = 9 * 9 * 8 * 7….

f(10) = 9 * 9 * 8 * 7 * 6 * … * 1

f(11) = 0 = f(12) = f(13)….

any number with length > 10 couldn’t be unique digits number.

The problem is asking for numbers from 0 to 10^n. Hence return f(1) + f(2) + .. + f(n)

As @4acreg suggests, There are only 11 different ans. You can create a lookup table for it. This problem is O(1) in essence.

public int countNumbersWithUniqueDigits(int n) {    if (n == 0)     return 1;    int res = 10;    int uniqueDigits = 9;    int availableNumber = 9;    while (n-- > 1 && availableNumber > 0) {        uniqueDigits = uniqueDigits * availableNumber;        res += uniqueDigits;        availableNumber--;    }    return res;}

Very simple 15-line backtrack solution

链接地址:https://discuss.leetcode.com/topic/54898/very-simple-15-line-backtrack-solution

这是个BackTracking,速度比我的快了10多倍。一定要背下来。

Thanks for sharing. I think it could be simplified further. This problem is kind of like permutation + subset, so we start from 0 every recursion and count through the path. Forgive me if anything unclear, here is the code:

public class Solution {    public int countNumbersWithUniqueDigits(int n) {        return doCount(n, new boolean[10], 0);    }    private int doCount(int n, boolean[] used, int d) {        if (d == n) return 1;        int total = 1;        for (int i = (d == 0) ? 1 : 0; i <= 9; i++) {            if (!used[i]) {                used[i] = true;                total += doCount(n, used, d + 1);                used[i] = false;            }        }        return total;    }}
0 0
原创粉丝点击