日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 【Leetcode】Count Numbers with Unique Digits

【Leetcode】Count Numbers with Unique Digits

來源:程序員人生   發布時間:2016-07-14 08:42:28 閱讀次數:2420次

題目鏈接: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].

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

算法:

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; }
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美成人精品一区二区男人看 | 久久免费av | 国产一区免费视频 | 日本福利在线 | 精品综合在线 | 色欧美日韩 | 狠狠一区 | 精品免费国产一区二区三区四区 | 欧美精品导航 | 欧洲中文字幕日韩精品成人 | 久久成人一区 | www.久| 亚洲精品在线观看免费 | 国产高清在线观看 | 成年人免费观看 | 国产精品不卡av | 欧美日韩高清 | 国产成人精品不卡 | 日本亚洲欧美 | 二区不卡 | 国产精品欧美一区二区 | 色婷婷亚洲精品 | 懂色av一区二区 | 美女黄视频免费 | 国产成人精品一区二区 | 精品国产精品国产偷麻豆 | 国产高清不卡 | 中文av在线播放 | 中文字幕在线观看日本 | 国产高清视频在线 | 日韩中文av | 精品久久一区 | 国产精品久久久久久久久久久久久久 | 国产一二三区在线观看 | 精品国产一 | 国产精品视频久久久 | 国产亚洲精品久久久优势 | 国产精品18久久久久久久网站 | 国产天堂 | 国产一区二区三区四 | 亚洲精品在线免费看 |