Super short & simple C++ 0ms solution


  • 2
    S
    int countNumbersWithUniqueDigits(int n)
    {
      if (n<1) return 1;
      // For single digit, we have 10 numbers with unique digits
      int totalUnique = 10;
      //After first digit, the choice for first digit is only 9 digits,
      int prevMultiplier = 9;
      for (int i = 1; i < n; i++)
      {
        prevMultiplier *= (10-i);
        totalUnique += prevMultiplier;
      }
      return totalUnique;
    }
    

  • 0
    R

    Shouldnt you be checking the condition for i<10 also?


  • 0
    S

    @rushabhmshah99 Why though? Here n is the number of digits. So, the for condition where the actual number is less than 10 is never required to be checked.


  • 0
    R

    I mean n could be greater than 10 and at that time 10-i will become negative, but my bad till then prevMultiplier will become 0.


Log in to reply
 

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