357 Count Numbers with Unique Digits
1. Question
2. Implementation
class Solution {
public int countNumbersWithUniqueDigits(int n) {
boolean[] used = new boolean[10];
int[] count = new int[1];
getNumbersWithUniqueDigits(0, n, used, count);
// 加1因为要算上刚开始的0
return 1 + count[0];
}
public void getNumbersWithUniqueDigits(int curLevel, int depth, boolean[] used, int[] count) {
if (curLevel == depth) {
return;
}
int start = curLevel == 0 ? 1 : 0;
for (int i = start; i < 10; i++) {
if (!used[i]) {
used[i] = true;
++count[0];
getNumbersWithUniqueDigits(curLevel + 1, depth, used, count);
used[i] = false;
}
}
}
}3. Time & Space Complexity
Last updated