C++ True O(1) solution


  • 0
    T
    class Solution {
        constexpr int generator(int const val) const
        {
            return (val == 0)?1:(val == 1)?9:(11-val)*generator(val-1);
        }
        
        constexpr int getValue(int const val) const
        {
            return (val == 0)?generator(0):generator(val) + getValue(val-1);
        }
    public:
        constexpr int countNumbersWithUniqueDigits(int k) 
        {
            return (k>=10)?0:getValue(k);
        }
    };
    

Log in to reply
 

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