Skip to content

Commit f828762

Browse files
committed
feat: add solutions to lc problem: No.1999
No.1999.Smallest Greater Multiple Made of Two Digits
1 parent 5b107ef commit f828762

File tree

6 files changed

+296
-2
lines changed

6 files changed

+296
-2
lines changed

solution/1900-1999/1999.Smallest Greater Multiple Made of Two Digits/README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,128 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:BFS**
61+
62+
我们观察 $k$ 的范围,发现 $1 \leq k \leq 1000$,因此,如果 $digit1$ 和 $digit2$ 都为 $0$,那么一定不存在满足条件的整数,直接返回 $-1$ 即可。
63+
64+
否则,我们不妨设 $digit1 \leq digit2$,接下来我们可以使用 BFS 的方法,初始时将整数 $0$ 入队,然后不断地从队首取出一个整数 $x$,如果 $x \gt 2^{31} - 1$,那么说明不存在满足条件的整数,直接返回 $-1$ 即可。如果 $x \gt k$ 且 $x \bmod k = 0$,那么说明找到了满足条件的整数,直接返回 $x$ 即可。否则,我们将其乘以 $10$ 后加上 $digit1$ 和 $digit2$,并将这两个整数入队,继续进行搜索。
65+
66+
时间复杂度 $(\log_{10} M)$,空间复杂度 $O(\log_{10} M)$,其中 $M$ 为 $2^{31} - 1$。
67+
6068
<!-- tabs:start -->
6169

6270
### **Python3**
6371

6472
<!-- 这里可写当前语言的特殊实现逻辑 -->
6573

6674
```python
67-
75+
class Solution:
76+
def findInteger(self, k: int, digit1: int, digit2: int) -> int:
77+
if digit1 == 0 and digit2 == 0:
78+
return -1
79+
if digit1 > digit2:
80+
return self.findInteger(k, digit2, digit1)
81+
q = deque([0])
82+
while 1:
83+
x = q.popleft()
84+
if x > 2**31 - 1:
85+
return -1
86+
if x > k and x % k == 0:
87+
return x
88+
q.append(x * 10 + digit1)
89+
if digit1 != digit2:
90+
q.append(x * 10 + digit2)
6891
```
6992

7093
### **Java**
7194

7295
<!-- 这里可写当前语言的特殊实现逻辑 -->
7396

7497
```java
98+
class Solution {
99+
public int findInteger(int k, int digit1, int digit2) {
100+
if (digit1 == 0 && digit2 == 0) {
101+
return -1;
102+
}
103+
if (digit1 > digit2) {
104+
return findInteger(k, digit2, digit1);
105+
}
106+
Deque<Long> q = new ArrayDeque<>();
107+
q.offer(0L);
108+
while (true) {
109+
long x = q.poll();
110+
if (x > Integer.MAX_VALUE) {
111+
return -1;
112+
}
113+
if (x > k && x % k == 0) {
114+
return (int) x;
115+
}
116+
q.offer(x * 10 + digit1);
117+
if (digit1 != digit2) {
118+
q.offer(x * 10 + digit2);
119+
}
120+
}
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int findInteger(int k, int digit1, int digit2) {
131+
if (digit1 == 0 && digit2 == 0) {
132+
return -1;
133+
}
134+
if (digit1 > digit2) {
135+
swap(digit1, digit2);
136+
}
137+
queue<long long> q{{0}};
138+
while (1) {
139+
long long x = q.front();
140+
q.pop();
141+
if (x > INT_MAX) {
142+
return -1;
143+
}
144+
if (x > k && x % k == 0) {
145+
return x;
146+
}
147+
q.emplace(x * 10 + digit1);
148+
if (digit1 != digit2) {
149+
q.emplace(x * 10 + digit2);
150+
}
151+
}
152+
}
153+
};
154+
```
75155
156+
### **Go**
157+
158+
```go
159+
func findInteger(k int, digit1 int, digit2 int) int {
160+
if digit1 == 0 && digit2 == 0 {
161+
return -1
162+
}
163+
if digit1 > digit2 {
164+
digit1, digit2 = digit2, digit1
165+
}
166+
q := []int{0}
167+
for {
168+
x := q[0]
169+
q = q[1:]
170+
if x > math.MaxInt32 {
171+
return -1
172+
}
173+
if x > k && x%k == 0 {
174+
return x
175+
}
176+
q = append(q, x*10+digit1)
177+
if digit1 != digit2 {
178+
q = append(q, x*10+digit2)
179+
}
180+
}
181+
}
76182
```
77183

78184
### **...**

solution/1900-1999/1999.Smallest Greater Multiple Made of Two Digits/README_EN.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,111 @@
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def findInteger(self, k: int, digit1: int, digit2: int) -> int:
63+
if digit1 == 0 and digit2 == 0:
64+
return -1
65+
if digit1 > digit2:
66+
return self.findInteger(k, digit2, digit1)
67+
q = deque([0])
68+
while 1:
69+
x = q.popleft()
70+
if x > 2**31 - 1:
71+
return -1
72+
if x > k and x % k == 0:
73+
return x
74+
q.append(x * 10 + digit1)
75+
if digit1 != digit2:
76+
q.append(x * 10 + digit2)
6277
```
6378

6479
### **Java**
6580

6681
```java
82+
class Solution {
83+
public int findInteger(int k, int digit1, int digit2) {
84+
if (digit1 == 0 && digit2 == 0) {
85+
return -1;
86+
}
87+
if (digit1 > digit2) {
88+
return findInteger(k, digit2, digit1);
89+
}
90+
Deque<Long> q = new ArrayDeque<>();
91+
q.offer(0L);
92+
while (true) {
93+
long x = q.poll();
94+
if (x > Integer.MAX_VALUE) {
95+
return -1;
96+
}
97+
if (x > k && x % k == 0) {
98+
return (int) x;
99+
}
100+
q.offer(x * 10 + digit1);
101+
if (digit1 != digit2) {
102+
q.offer(x * 10 + digit2);
103+
}
104+
}
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int findInteger(int k, int digit1, int digit2) {
115+
if (digit1 == 0 && digit2 == 0) {
116+
return -1;
117+
}
118+
if (digit1 > digit2) {
119+
swap(digit1, digit2);
120+
}
121+
queue<long long> q{{0}};
122+
while (1) {
123+
long long x = q.front();
124+
q.pop();
125+
if (x > INT_MAX) {
126+
return -1;
127+
}
128+
if (x > k && x % k == 0) {
129+
return x;
130+
}
131+
q.emplace(x * 10 + digit1);
132+
if (digit1 != digit2) {
133+
q.emplace(x * 10 + digit2);
134+
}
135+
}
136+
}
137+
};
138+
```
67139
140+
### **Go**
141+
142+
```go
143+
func findInteger(k int, digit1 int, digit2 int) int {
144+
if digit1 == 0 && digit2 == 0 {
145+
return -1
146+
}
147+
if digit1 > digit2 {
148+
digit1, digit2 = digit2, digit1
149+
}
150+
q := []int{0}
151+
for {
152+
x := q[0]
153+
q = q[1:]
154+
if x > math.MaxInt32 {
155+
return -1
156+
}
157+
if x > k && x%k == 0 {
158+
return x
159+
}
160+
q = append(q, x*10+digit1)
161+
if digit1 != digit2 {
162+
q = append(q, x*10+digit2)
163+
}
164+
}
165+
}
68166
```
69167

70168
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int findInteger(int k, int digit1, int digit2) {
4+
if (digit1 == 0 && digit2 == 0) {
5+
return -1;
6+
}
7+
if (digit1 > digit2) {
8+
swap(digit1, digit2);
9+
}
10+
queue<long long> q{{0}};
11+
while (1) {
12+
long long x = q.front();
13+
q.pop();
14+
if (x > INT_MAX) {
15+
return -1;
16+
}
17+
if (x > k && x % k == 0) {
18+
return x;
19+
}
20+
q.emplace(x * 10 + digit1);
21+
if (digit1 != digit2) {
22+
q.emplace(x * 10 + digit2);
23+
}
24+
}
25+
}
26+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func findInteger(k int, digit1 int, digit2 int) int {
2+
if digit1 == 0 && digit2 == 0 {
3+
return -1
4+
}
5+
if digit1 > digit2 {
6+
digit1, digit2 = digit2, digit1
7+
}
8+
q := []int{0}
9+
for {
10+
x := q[0]
11+
q = q[1:]
12+
if x > math.MaxInt32 {
13+
return -1
14+
}
15+
if x > k && x%k == 0 {
16+
return x
17+
}
18+
q = append(q, x*10+digit1)
19+
if digit1 != digit2 {
20+
q = append(q, x*10+digit2)
21+
}
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int findInteger(int k, int digit1, int digit2) {
3+
if (digit1 == 0 && digit2 == 0) {
4+
return -1;
5+
}
6+
if (digit1 > digit2) {
7+
return findInteger(k, digit2, digit1);
8+
}
9+
Deque<Long> q = new ArrayDeque<>();
10+
q.offer(0L);
11+
while (true) {
12+
long x = q.poll();
13+
if (x > Integer.MAX_VALUE) {
14+
return -1;
15+
}
16+
if (x > k && x % k == 0) {
17+
return (int) x;
18+
}
19+
q.offer(x * 10 + digit1);
20+
if (digit1 != digit2) {
21+
q.offer(x * 10 + digit2);
22+
}
23+
}
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def findInteger(self, k: int, digit1: int, digit2: int) -> int:
3+
if digit1 == 0 and digit2 == 0:
4+
return -1
5+
if digit1 > digit2:
6+
return self.findInteger(k, digit2, digit1)
7+
q = deque([0])
8+
while 1:
9+
x = q.popleft()
10+
if x > 2**31 - 1:
11+
return -1
12+
if x > k and x % k == 0:
13+
return x
14+
q.append(x * 10 + digit1)
15+
if digit1 != digit2:
16+
q.append(x * 10 + digit2)

0 commit comments

Comments
 (0)