C++,O(1)


  • 0
    V

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


  • 0
    R

    Your code runs O(n) time. Before the loop, do something like n = min(n, 10) to make sure it is O(1).


Log in to reply
 

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