Such a simple thing, mentality exploded, the first glance will know very simple, originally intended to do half an hour, the results tamper tamper and nearly 50 minutes, but also in the middle of debugging several times, the algorithm is not really can’t.
AC after the state of mind and exploded, this know this is a digit dp, but I use greed (turns out not greedy), thought for a long time also did not think of how to do with dp, in the burst of time, the original dp is to store the value of one place, ten place just
int countNumbersWithUniqueDigits(int n) {
if(0==n)
return 1;
if(n<0 || n>10)
return 0;
int ret = 0;
for(int i=n;i>=1;--i)
{
int ret1 = 1;
int num = 9;
for(int j=0;j<i;++j)
{
ret1 *= num;
num = 0==j ?num : num-1;
}
ret += ret1;
}
return ret+1;
}
Read More:
- PHP: How to get the total number of data in the MySQL table (total number of rows)
- PHP: How to parse MHT file into HTML
- How to solve Magento 500 internal server errors
- How to Hide index.php
- How to Solve PHP Fatal error: Namespace declaration statement has to be the very first statement
- How to Solve opcache error: zend_mm_heap corrupted
- How to Solve phpExcel Error: ERR_INVALID_RESPONSE
- php_network_getaddresses: getaddrinfo failed (How to Fix)
- How to Solve PHP Error: no package’oniguruma’ found
- Using $this when not in object context in (How to Fix)
- [Solved] PHPMailer Failed to Send Email: SMTP Error: Could not connect to SMTP host.
- [Solved] WordPress Upgrade PHP 5.6 to 7.x Fatal error: Uncaught Error: Call to undefined function mysql_connect()
- php Error: mail() Failed to connect to mailserver at “localhost” port 25
- [Solved] PHPMailer Error: SMTP ERROR: Failed to connect to server: (0)
- PHP Error Object of class mysqli could not be converted to string in
- [Solved] Warning: Unknown: Failed to write session data using user defined save handler. (session.save_path:
- [Solved] cURL error 60: SSL certificate problem: unable to get local issuer certificate
- [Solved] PHP Error: Warning: file_get_contents(): Failed to enable crypto
- [Solved] Type error: Argument 1 passed to Monolog\Utils::getClass() must be an instance of Monolog\object
- [Solved] ‘node’ command failed to start the project, with an error: “unknown database’ mydb ‘”