Skip to content

Commit 574af9d

Browse files
authored
feat: add solutions to lc problem: No.3032 (doocs#2331)
1 parent af8bf48 commit 574af9d

File tree

7 files changed

+291
-0
lines changed

7 files changed

+291
-0
lines changed

solution/3000-3099/3032.Count Numbers With Unique Digits II/README.md

+104
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,108 @@ function numberCount(a: number, b: number): number {
257257

258258
<!-- tabs:end -->
259259

260+
### 方法 2
261+
262+
<!-- tabs:start -->
263+
264+
```python
265+
class Solution:
266+
def numberCount(self, a: int, b: int) -> int:
267+
return sum(len(set(str(num))) == len(str(num)) for num in range(a, b + 1))
268+
```
269+
270+
```java
271+
class Solution {
272+
public int numberCount(int a, int b) {
273+
int res = 0;
274+
for (int i = a; i <= b; ++i) {
275+
if (isValid(i)) {
276+
++res;
277+
}
278+
}
279+
return res;
280+
}
281+
private boolean isValid(int n) {
282+
boolean[] present = new boolean[10];
283+
Arrays.fill(present, false);
284+
while (n > 0) {
285+
int dig = n % 10;
286+
if (present[dig]) {
287+
return false;
288+
}
289+
present[dig] = true;
290+
n /= 10;
291+
}
292+
return true;
293+
}
294+
}
295+
```
296+
297+
```cpp
298+
class Solution {
299+
public:
300+
bool isvalid(int n) {
301+
vector<bool> present(10, false);
302+
while (n) {
303+
const int dig = n % 10;
304+
if (present[dig])
305+
return false;
306+
present[dig] = true;
307+
n /= 10;
308+
}
309+
return true;
310+
}
311+
int numberCount(int a, int b) {
312+
int res = 0;
313+
for (int i = a; i <= b; ++i) {
314+
if (isvalid(i)) {
315+
++res;
316+
}
317+
}
318+
return res;
319+
}
320+
};
321+
```
322+
323+
```go
324+
func numberCount(a int, b int) int {
325+
count := 0
326+
for num := a; num <= b; num++ {
327+
if hasUniqueDigits(num) {
328+
count++
329+
}
330+
}
331+
return count
332+
}
333+
func hasUniqueDigits(num int) bool {
334+
digits := strconv.Itoa(num)
335+
seen := make(map[rune]bool)
336+
for _, digit := range digits {
337+
if seen[digit] {
338+
return false
339+
}
340+
seen[digit] = true
341+
}
342+
return true
343+
}
344+
```
345+
346+
```ts
347+
function numberCount(a: number, b: number): number {
348+
let count: number = 0;
349+
for (let num = a; num <= b; num++) {
350+
if (hasUniqueDigits(num)) {
351+
count++;
352+
}
353+
}
354+
return count;
355+
}
356+
function hasUniqueDigits(num: number): boolean {
357+
const digits: Set<string> = new Set(num.toString().split(''));
358+
return digits.size === num.toString().length;
359+
}
360+
```
361+
362+
<!-- tabs:end -->
363+
260364
<!-- end -->

solution/3000-3099/3032.Count Numbers With Unique Digits II/README_EN.md

+104
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,108 @@ function numberCount(a: number, b: number): number {
255255

256256
<!-- tabs:end -->
257257

258+
### Solution 2
259+
260+
<!-- tabs:start -->
261+
262+
```python
263+
class Solution:
264+
def numberCount(self, a: int, b: int) -> int:
265+
return sum(len(set(str(num))) == len(str(num)) for num in range(a, b + 1))
266+
```
267+
268+
```java
269+
class Solution {
270+
public int numberCount(int a, int b) {
271+
int res = 0;
272+
for (int i = a; i <= b; ++i) {
273+
if (isValid(i)) {
274+
++res;
275+
}
276+
}
277+
return res;
278+
}
279+
private boolean isValid(int n) {
280+
boolean[] present = new boolean[10];
281+
Arrays.fill(present, false);
282+
while (n > 0) {
283+
int dig = n % 10;
284+
if (present[dig]) {
285+
return false;
286+
}
287+
present[dig] = true;
288+
n /= 10;
289+
}
290+
return true;
291+
}
292+
}
293+
```
294+
295+
```cpp
296+
class Solution {
297+
public:
298+
bool isvalid(int n) {
299+
vector<bool> present(10, false);
300+
while (n) {
301+
const int dig = n % 10;
302+
if (present[dig])
303+
return false;
304+
present[dig] = true;
305+
n /= 10;
306+
}
307+
return true;
308+
}
309+
int numberCount(int a, int b) {
310+
int res = 0;
311+
for (int i = a; i <= b; ++i) {
312+
if (isvalid(i)) {
313+
++res;
314+
}
315+
}
316+
return res;
317+
}
318+
};
319+
```
320+
321+
```go
322+
func numberCount(a int, b int) int {
323+
count := 0
324+
for num := a; num <= b; num++ {
325+
if hasUniqueDigits(num) {
326+
count++
327+
}
328+
}
329+
return count
330+
}
331+
func hasUniqueDigits(num int) bool {
332+
digits := strconv.Itoa(num)
333+
seen := make(map[rune]bool)
334+
for _, digit := range digits {
335+
if seen[digit] {
336+
return false
337+
}
338+
seen[digit] = true
339+
}
340+
return true
341+
}
342+
```
343+
344+
```ts
345+
function numberCount(a: number, b: number): number {
346+
let count: number = 0;
347+
for (let num = a; num <= b; num++) {
348+
if (hasUniqueDigits(num)) {
349+
count++;
350+
}
351+
}
352+
return count;
353+
}
354+
function hasUniqueDigits(num: number): boolean {
355+
const digits: Set<string> = new Set(num.toString().split(''));
356+
return digits.size === num.toString().length;
357+
}
358+
```
359+
360+
<!-- tabs:end -->
361+
258362
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool isvalid(int n) {
4+
vector<bool> present(10, false);
5+
while (n) {
6+
const int dig = n % 10;
7+
if (present[dig])
8+
return false;
9+
present[dig] = true;
10+
n /= 10;
11+
}
12+
return true;
13+
}
14+
int numberCount(int a, int b) {
15+
int res = 0;
16+
for (int i = a; i <= b; ++i) {
17+
if (isvalid(i)) {
18+
++res;
19+
}
20+
}
21+
return res;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func numberCount(a int, b int) int {
2+
count := 0
3+
for num := a; num <= b; num++ {
4+
if hasUniqueDigits(num) {
5+
count++
6+
}
7+
}
8+
return count
9+
}
10+
func hasUniqueDigits(num int) bool {
11+
digits := strconv.Itoa(num)
12+
seen := make(map[rune]bool)
13+
for _, digit := range digits {
14+
if seen[digit] {
15+
return false
16+
}
17+
seen[digit] = true
18+
}
19+
return true
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int numberCount(int a, int b) {
3+
int res = 0;
4+
for (int i = a; i <= b; ++i) {
5+
if (isValid(i)) {
6+
++res;
7+
}
8+
}
9+
return res;
10+
}
11+
private boolean isValid(int n) {
12+
boolean[] present = new boolean[10];
13+
Arrays.fill(present, false);
14+
while (n > 0) {
15+
int dig = n % 10;
16+
if (present[dig]) {
17+
return false;
18+
}
19+
present[dig] = true;
20+
n /= 10;
21+
}
22+
return true;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def numberCount(self, a: int, b: int) -> int:
3+
return sum(len(set(str(num))) == len(str(num)) for num in range(a, b + 1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function numberCount(a: number, b: number): number {
2+
let count: number = 0;
3+
for (let num = a; num <= b; num++) {
4+
if (hasUniqueDigits(num)) {
5+
count++;
6+
}
7+
}
8+
return count;
9+
}
10+
function hasUniqueDigits(num: number): boolean {
11+
const digits: Set<string> = new Set(num.toString().split(''));
12+
return digits.size === num.toString().length;
13+
}

0 commit comments

Comments
 (0)