Java solution 0 ms


  • 0
    H
        public class Solution {
        static final int[] np = {0, 9, 72, 504, 3024, 15120, 60480, 181440, 362880, 362880};
        public int countNumbersWithUniqueDigits(int n) {
            int ans = 1; // To cover 0 itself
            n = Math.min(n, 10);
            for (int i = 1; i <= n; i++) {
                // Case 1: 0 is one of the digits except in the MSD.
                // (i-1) positions 0 can be present, the remaining (i-1) digits can take => 9 * 8 * .. 9-i-1 values
                // Case 2: no 0 at all in any digits => 9 * 8 * .... 9-i possibilities.
                ans += (np[i-1])*(i-1) + np[i];
            }
            return ans;
        }
    }

Log in to reply
 

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