Skip to content

Commit cb0f7b0

Browse files
edit 357
1 parent 4ef208b commit cb0f7b0

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

README.md

+1-1
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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package com.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
public class _357 {
1821

1922
public int countNumbersWithUniqueDigits(int n) {
20-
if (n == 0) return 1;
23+
if (n == 0) {
24+
return 1;
25+
}
2126
int res = 10;
2227
int uniqueDigits = 9;
2328
int availableNumber = 9;

0 commit comments

Comments
 (0)