O(1) space:

```
int countNumbersWithUniqueDigits(int n) {
if ( n < 0 ) return 0;
int result = 1;
int multiplier = 9;
n = min(n, 10);
for (int i = 1; i <= n; i++) {
result += multiplier;
multiplier *= (i > 9 ? 0: (10 - i));
}
return result;
}
```

O(n) space:

```
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if ( n < 0 ) return 0;
vector<int> result(n+1);
result[0] = 1;
int multiplier = 9;
n = min(n, 10);
for (int i = 1; i <= n; i++) {
result[i] = result[i-1] + multiplier;
multiplier *= (i > 9 ? 0: (10 - i));
}
return result[n];
}
};
```