Leetcode Count Numbers with Unique Digits

Source: Internet
Author: User

Title Description given a non-negative integer n, count all numbers with unique digits, X,where 0≤x <10n. Example:given N=2,return  the. (The answer should is the total numbersinchThe range of0≤x < -, excluding [ One, A, -, -, -, the, the, the, About]) Hint:1. A Direct isTo use the backtracking approach.2. Backtracking should contains three states which is (the current number,
Number of steps toGetThat number and a bitmask which represent which number isMarked asVisited so farinchThe current number).
Start with State (0,0,0) and count all valid number till we reach number of steps equals to 10n.3. This problem can also is solvedusingADynamicprogramming approach and some knowledge of combinatorics.4. Let f (k) =count of numbers with a unique digits with length equals K.5. F (1) =Ten,..., f (k) =9*9*8* ... (9-K +2) [The first factor is 9Because a number cannot start with0].

That is, given an integer n, calculates the number of all numbers that do not contain duplicates from [0^n]

1. Backtracking:

If you enter N, there is a total of n digits regardless of the 10^n. Use Cur[n] to represent the current number. Use a flag[10] to represent 0~9 digits, each time a number is placed, the flag corresponds to a number of 1,

The code is as follows:

classsolution (object):defcountnumberswithuniquedigits (self, n):""": Type N:int:rtype:int"""        ifn = =0:return1Flag= [0]*10cur= [0]*N Step=0; Count=[0]; Self.helper (0, flag, cur, count, N)returnCOUNT[0]defHelper (self, step, flag, cur, COUNT, N):ifStep >=N:count[0]+ = 1return         forIinchRange (0, 10):            ifFlag[i]! = 1or(i==0 andStep!=n andSUM (cur[step:]) = =0): #解决诸如 [0,0,0], [0, 1,0] Such numbers do not count the question Cur[step]=I flag[i]= 1Self.helper (Step+1, flag, cur, count, N) flag[i]=0Else:                Continue

The above backtracking method timed out in time t_t

2. Dynamic Planning:

According to Tip 4, f (k) = 9 * 9 * ... (9-k+2)

classsolution (object):defcountnumberswithuniquedigits (self, n):""": Type N:int:rtype:int"""        ifn = =0:return1ifn = = 1:            return10Res=0 forIinchRange (1, n+1): Res+=Self.count (i)returnResdefcount (self, num):ifnum = = 0:return1ifnum = = 1:return10Res= 1 forIinchRange (9, 11-num-1, 1): Res*=IreturnRes*9

Leetcode Count Numbers with Unique Digits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.