多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開源 > php教程 > 【Leetcode】Count Numbers with Unique Digits

【Leetcode】Count Numbers with Unique Digits

來(lái)源:程序員人生   發(fā)布時(shí)間:2016-07-14 08:42:28 閱讀次數(shù):2417次

題目鏈接:https://leetcode.com/problems/count-numbers-with-unique-digits/

題目:
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

Hint:

A direct way is to use the backtracking approach.
Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10n.
This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
Let f(k) = count of numbers with unique digits with length equals k.
f(1) = 10, …, f(k) = 9 * 9 * 8 * … (9 - k + 2) [The first factor is 9 because a number cannot start with 0].

思路:
遞推方程都給了,傻子采取回溯。。= =。。而且回溯看起來(lái)好難的模樣。。

算法:

public int countNumbersWithUniqueDigits(int n) { if (n == 0) return 1; int count = 0; for (int i = 1; i <= n; i++) {// 求每位有多少unique digits count += cal(i); } return count; } public int cal(int k) { int sum = 9; if (k == 0) return 0; if (k == 1) return 10; for (int i = 11 - k; i <= 9; i++) { sum *= i; } return sum; }
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 伊久久| 久久久免费精品视频 | 999毛片免费 | 欧美性猛交xxxx乱大交中文 | 老女人一级全黄肉体毛片 | 中文字幕第二十页 | 多人做人爱视频大全在线观看 | 国产中文字幕在线视频 | 伊人中文 | 久久a毛片| a级片网| 亚洲 欧美 日韩 综合 | 欧美一区永久视频免费观看 | 久久综合九色综合桃花 | 一级特黄女人生活片 | 97精品伊人久久大香线蕉 | 中文字幕日本不卡一二三区 | 久久天天躁夜夜躁狠狠85台湾 | 国产日韩久久久精品影院首页 | 自拍偷拍亚洲区 | 美女毛片网站 | 久久这里只有精品9 | 亚洲精品欧美日韩 | 国产精品一级视频 | 一区二区三区四区国产精品 | 成人精品视频一区二区在线 | 91福利片| 亚洲国产成人久久一区二区三区 | 国产亚洲欧美日韩国产片 | 亚洲日韩欧美综合 | 亚洲欧美手机在线观看 | 在线不卡免费视频 | 一级特黄女人生活片 | 国产男人女人做性全过程视频 | 国产国语一级a毛片高清视频 | 2022国产男人亚洲欧美天堂 | 欧美白人黑人xxxx猛交 | 久久精品国产精品亚洲人人 | 最新中文字幕 | 亚洲视频一区二区三区 | 欧美另类69xxxxx性欧 |