Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcb0f7b0

Browse files
edit 357
1 parent4ef208b commitcb0f7b0

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Your ideas/fixes/algorithms are more than welcome!
252252
|360|[Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_360.java)| O(n)|O(1) | Medium| Two Pointers, Math
253253
|359|[Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_359.java)| amortized O(1)|O(k) | Easy| HashMap
254254
|358|[Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_358.java)| O(n)|O(n) | Hard| HashMap, Heap, Greedy
255-
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_357.java)| O(?)|O(?)| Medium|
255+
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_357.java)| O(n)|O(1) | Medium| DP, Math
256256
|356|[Line Reflection](https://leetcode.com/problems/line-reflection/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_356.java)| O(n)|O(n) | Medium| HashSet
257257
|355|[Design Twitter](https://leetcode.com/problems/design-twitter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_355.java)| O(n)|O(n) | Medium| Design, HashMap, Heap
258258
|354|[Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_354.java)| O(nlogn)|O(1) | Hard| DP, Binary Search

‎src/main/java/com/fishercoder/solutions/_357.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
packagecom.fishercoder.solutions;
22

33
/**
4+
* 357. Count Numbers with Unique Digits
45
* Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
56
67
Example:
78
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])
89
910
Hint:
10-
1111
A direct way is to use the backtracking approach.
12-
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.
12+
Backtracking should contains three states which are
13+
(the current number, number of steps to get that number and a bitmask which
14+
represent which number is marked as visited so far in the current number).
15+
Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10n.
1316
This problem can also be solved using a dynamic programming approach and some knowledge of combinatorics.
1417
Let f(k) = count of numbers with unique digits with length equals k.
1518
f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].
1619
*/
1720
publicclass_357 {
1821

1922
publicintcountNumbersWithUniqueDigits(intn) {
20-
if (n ==0)return1;
23+
if (n ==0) {
24+
return1;
25+
}
2126
intres =10;
2227
intuniqueDigits =9;
2328
intavailableNumber =9;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp