N = 0, return 2 test case Wrong?


  • -2
    J

    n digits unique number is: 9 * 9 * 8 ... * (11 - n), below code is AC, but not sure why n = 0 should return 2.

    public class Solution {
        public int countNumbersWithUniqueDigits(int n) {
            if (n <= 0) return 2; // when n = 0, there is 0 and 1
            int c = 9, res = 0;
            for (int i = 1; i <= n && i <= 10; i++) {
                if (i == 1) res += c + 1; // as from hpplayer below, n = 1, should be 11 (c + 2 here) as 0 ~10 all valid
                else {
                    c *= 11 - i;
                    res += c;
                }
            }
            return res;
        }
    }

  • 1
    H

    n = 0 => counting from 0 to 10^0 = > counting from 0 to 1 => both 0 and 1 are unique digits => return 2

    What makes me confused is the case n = 1

    n = 1 => counting from 0 to 10^1 => counting from 0 to 10 => we have 11 nums with unique digits => should return 11 but 10 is expected??


  • 0
    J

    I think you are right, n = 0, 1 need to treat special, n = 1 should be 11 instead of 10


  • 0
    H
    This post is deleted!

  • 0

    I have revised the condition to 0 <= x < 10^n (Note that it does not include 10^n). Therefore the answer for n=0 and n=1 are 1 and 10 respectively.


  • 0
    I

    in this question 0 <= x < 10^n , x is less than 10^n. So we are counting from 0 to 9.


  • 0
    H

    Thx for replay. Now the description is updated. Previously, the question states 0 <= x <= 10^n


Log in to reply
 

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