Skip to content

Commit 475d2a1

Browse files
committed
feat: add solutions to lc problem: No.2059
No.2059.Minimum Operations to Convert Number
1 parent da218b8 commit 475d2a1

File tree

3 files changed

+278
-4
lines changed

3 files changed

+278
-4
lines changed

solution/2000-2099/2059.Minimum Operations to Convert Number/README.md

+139-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
<!-- 这里可写通用的实现逻辑 -->
9191

92-
BFS
92+
BFS 最小步数模型。
9393

9494
<!-- tabs:start -->
9595

@@ -113,12 +113,39 @@ class Solution:
113113
nx = op(x, num)
114114
if nx == goal:
115115
return step + 1
116-
if nx >= 0 and nx <= 1000 and not vis[nx]:
116+
if 0 <= nx <= 1000 and not vis[nx]:
117117
q.append((nx, step + 1))
118118
vis[nx] = True
119119
return -1
120120
```
121121

122+
```python
123+
class Solution:
124+
def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
125+
def next(x):
126+
res = []
127+
for num in nums:
128+
res.append(x + num)
129+
res.append(x - num)
130+
res.append(x ^ num)
131+
return res
132+
133+
q = deque([start])
134+
vis = set([(start)])
135+
ans = 0
136+
while q:
137+
ans += 1
138+
for _ in range(len(q), 0, -1):
139+
x = q.popleft()
140+
for y in next(x):
141+
if y == goal:
142+
return ans
143+
if 0 <= y <= 1000 and y not in vis:
144+
vis.add(y)
145+
q.append(y)
146+
return -1
147+
```
148+
122149
### **Java**
123150

124151
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -154,6 +181,43 @@ class Solution {
154181
}
155182
```
156183

184+
```java
185+
class Solution {
186+
public int minimumOperations(int[] nums, int start, int goal) {
187+
Deque<Integer> q = new ArrayDeque<>();
188+
q.offer(start);
189+
boolean[] vis = new boolean[1001];
190+
int ans = 0;
191+
while (!q.isEmpty()) {
192+
++ans;
193+
for (int n = q.size(); n > 0; --n) {
194+
int x = q.poll();
195+
for (int y : next(nums, x)) {
196+
if (y == goal) {
197+
return ans;
198+
}
199+
if (y >= 0 && y <= 1000 && !vis[y]) {
200+
vis[y] = true;
201+
q.offer(y);
202+
}
203+
}
204+
}
205+
}
206+
return -1;
207+
}
208+
209+
private List<Integer> next(int[] nums, int x) {
210+
List<Integer> res = new ArrayList<>();
211+
for (int num : nums) {
212+
res.add(x + num);
213+
res.add(x - num);
214+
res.add(x ^ num);
215+
}
216+
return res;
217+
}
218+
}
219+
```
220+
157221
### **C++**
158222

159223
```cpp
@@ -190,6 +254,47 @@ public:
190254
};
191255
```
192256
257+
```cpp
258+
class Solution {
259+
public:
260+
int minimumOperations(vector<int>& nums, int start, int goal) {
261+
queue<int> q{{start}};
262+
vector<bool> vis(1001);
263+
int ans = 0;
264+
while (!q.empty())
265+
{
266+
++ans;
267+
for (int n = q.size(); n > 0; --n)
268+
{
269+
int x = q.front();
270+
q.pop();
271+
for (int y : next(nums, x))
272+
{
273+
if (y == goal) return ans;
274+
if (y >= 0 && y <= 1000 && !vis[y])
275+
{
276+
vis[y] = true;
277+
q.push(y);
278+
}
279+
}
280+
}
281+
}
282+
return -1;
283+
}
284+
285+
vector<int> next(vector<int>& nums, int x) {
286+
vector<int> res;
287+
for (int num : nums)
288+
{
289+
res.push_back(x + num);
290+
res.push_back(x - num);
291+
res.push_back(x ^ num);
292+
}
293+
return res;
294+
}
295+
};
296+
```
297+
193298
### **Go**
194299

195300
```go
@@ -227,6 +332,38 @@ func minimumOperations(nums []int, start int, goal int) int {
227332
}
228333
```
229334

335+
```go
336+
func minimumOperations(nums []int, start int, goal int) int {
337+
next := func(x int) []int {
338+
var res []int
339+
for _, num := range nums {
340+
res = append(res, []int{x + num, x - num, x ^ num}...)
341+
}
342+
return res
343+
}
344+
q := []int{start}
345+
vis := make([]bool, 1001)
346+
ans := 0
347+
for len(q) > 0 {
348+
ans++
349+
for n := len(q); n > 0; n-- {
350+
x := q[0]
351+
q = q[1:]
352+
for _, y := range next(x) {
353+
if y == goal {
354+
return ans
355+
}
356+
if y >= 0 && y <= 1000 && !vis[y] {
357+
vis[y] = true
358+
q = append(q, y)
359+
}
360+
}
361+
}
362+
}
363+
return -1
364+
}
365+
```
366+
230367
### **TypeScript**
231368

232369
```ts

solution/2000-2099/2059.Minimum Operations to Convert Number/README_EN.md

+138-1
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,39 @@ class Solution:
108108
nx = op(x, num)
109109
if nx == goal:
110110
return step + 1
111-
if nx >= 0 and nx <= 1000 and not vis[nx]:
111+
if 0 <= nx <= 1000 and not vis[nx]:
112112
q.append((nx, step + 1))
113113
vis[nx] = True
114114
return -1
115115
```
116116

117+
```python
118+
class Solution:
119+
def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
120+
def next(x):
121+
res = []
122+
for num in nums:
123+
res.append(x + num)
124+
res.append(x - num)
125+
res.append(x ^ num)
126+
return res
127+
128+
q = deque([start])
129+
vis = set([(start)])
130+
ans = 0
131+
while q:
132+
ans += 1
133+
for _ in range(len(q), 0, -1):
134+
x = q.popleft()
135+
for y in next(x):
136+
if y == goal:
137+
return ans
138+
if 0 <= y <= 1000 and y not in vis:
139+
vis.add(y)
140+
q.append(y)
141+
return -1
142+
```
143+
117144
### **Java**
118145

119146
```java
@@ -147,6 +174,43 @@ class Solution {
147174
}
148175
```
149176

177+
```java
178+
class Solution {
179+
public int minimumOperations(int[] nums, int start, int goal) {
180+
Deque<Integer> q = new ArrayDeque<>();
181+
q.offer(start);
182+
boolean[] vis = new boolean[1001];
183+
int ans = 0;
184+
while (!q.isEmpty()) {
185+
++ans;
186+
for (int n = q.size(); n > 0; --n) {
187+
int x = q.poll();
188+
for (int y : next(nums, x)) {
189+
if (y == goal) {
190+
return ans;
191+
}
192+
if (y >= 0 && y <= 1000 && !vis[y]) {
193+
vis[y] = true;
194+
q.offer(y);
195+
}
196+
}
197+
}
198+
}
199+
return -1;
200+
}
201+
202+
private List<Integer> next(int[] nums, int x) {
203+
List<Integer> res = new ArrayList<>();
204+
for (int num : nums) {
205+
res.add(x + num);
206+
res.add(x - num);
207+
res.add(x ^ num);
208+
}
209+
return res;
210+
}
211+
}
212+
```
213+
150214
### **C++**
151215

152216
```cpp
@@ -183,6 +247,47 @@ public:
183247
};
184248
```
185249
250+
```cpp
251+
class Solution {
252+
public:
253+
int minimumOperations(vector<int>& nums, int start, int goal) {
254+
queue<int> q{{start}};
255+
vector<bool> vis(1001);
256+
int ans = 0;
257+
while (!q.empty())
258+
{
259+
++ans;
260+
for (int n = q.size(); n > 0; --n)
261+
{
262+
int x = q.front();
263+
q.pop();
264+
for (int y : next(nums, x))
265+
{
266+
if (y == goal) return ans;
267+
if (y >= 0 && y <= 1000 && !vis[y])
268+
{
269+
vis[y] = true;
270+
q.push(y);
271+
}
272+
}
273+
}
274+
}
275+
return -1;
276+
}
277+
278+
vector<int> next(vector<int>& nums, int x) {
279+
vector<int> res;
280+
for (int num : nums)
281+
{
282+
res.push_back(x + num);
283+
res.push_back(x - num);
284+
res.push_back(x ^ num);
285+
}
286+
return res;
287+
}
288+
};
289+
```
290+
186291
### **Go**
187292

188293
```go
@@ -220,6 +325,38 @@ func minimumOperations(nums []int, start int, goal int) int {
220325
}
221326
```
222327

328+
```go
329+
func minimumOperations(nums []int, start int, goal int) int {
330+
next := func(x int) []int {
331+
var res []int
332+
for _, num := range nums {
333+
res = append(res, []int{x + num, x - num, x ^ num}...)
334+
}
335+
return res
336+
}
337+
q := []int{start}
338+
vis := make([]bool, 1001)
339+
ans := 0
340+
for len(q) > 0 {
341+
ans++
342+
for n := len(q); n > 0; n-- {
343+
x := q[0]
344+
q = q[1:]
345+
for _, y := range next(x) {
346+
if y == goal {
347+
return ans
348+
}
349+
if y >= 0 && y <= 1000 && !vis[y] {
350+
vis[y] = true
351+
q = append(q, y)
352+
}
353+
}
354+
}
355+
}
356+
return -1
357+
}
358+
```
359+
223360
### **TypeScript**
224361

225362
```ts

solution/2000-2099/2059.Minimum Operations to Convert Number/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
1313
nx = op(x, num)
1414
if nx == goal:
1515
return step + 1
16-
if nx >= 0 and nx <= 1000 and not vis[nx]:
16+
if 0 <= nx <= 1000 and not vis[nx]:
1717
q.append((nx, step + 1))
1818
vis[nx] = True
1919
return -1

0 commit comments

Comments
 (0)