May be the OJ should put more test cases, what if n >= 10?


  • 0
    J

    What if n >= 10? I think we should consider this kind of input to make our code more robust.

    class Solution {
    public:
        int countNumbersWithUniqueDigits(int n) {
    		int res[11];
    		res[0] = 1;
    		res[1] = 10;
    		for(int i = 2; i <= n && i <= 10; ++i) {
    			int base = 9, temp = 9;
    			while(temp >= 11 - i)
    				base *= temp--;
    			res[i] = res[i - 1] + base;
    		}
    		return n <= 10 ? res[n] : res[10];
        }
    };
    

  • 0

    @JulianWang12 said in May be the OJ should put more test cases, what if n >= 10?:

    class Solution {
    public:
    int countNumbersWithUniqueDigits(int n) {
    int res[11];
    res[0] = 1;
    res[1] = 10;
    for(int i = 2; i <= n && i <= 10; ++i) {
    int base = 9, temp = 9;
    while(temp >= 11 - i)
    base *= temp--;
    res[i] = res[i - 1] + base;
    }
    return n <= 10 ? res[n] : res[10];
    }
    };

    What do you mean exactly here? I just test it using 20, it just works fine.


  • 0

    And here is my testing solution

    class Solution {
    public:
        int countNumbersWithUniqueDigits(int n) {
            if(n == 0) return 1;
            int sum = 10;
            for(int i = 2; i <= n; ++i)
            {
                int t = 9;
                for(int j = 0; j < i-1; ++j) t *= (9-j);
                sum += t;
            }
            return sum;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.