Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7de7f6

Browse files
feat: add solutions to lc problem: No.2059
No.2059.Minimum Operations to Convert Number
1 parent 7aee778 commit b7de7f6

File tree

6 files changed

+353
-7
lines changed

6 files changed

+353
-7
lines changed

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

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<strong>输出:</strong>3
6969
<strong>解释:</strong>
7070
可以按 0 → 1 → 2 → 3 的转化路径进行,只需执行下述 3 次运算:
71-
- 0 + 1 = 1
71+
- 0 + 1 = 1
7272
- 1 + 1 = 2
7373
- 2 + 1 = 3
7474
</pre>
@@ -98,15 +98,133 @@ BFS
9898
<!-- 这里可写当前语言的特殊实现逻辑 -->
9999

100100
```python
101-
101+
class Solution:
102+
def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
103+
op1 = lambda x, y: x + y
104+
op2 = lambda x, y: x - y
105+
op3 = lambda x, y: x ^ y
106+
ops = [op1, op2, op3]
107+
vis = [False] * 1001
108+
q = deque([(start, 0)])
109+
while q:
110+
x, step = q.popleft()
111+
for num in nums:
112+
for op in ops:
113+
nx = op(x, num)
114+
if nx == goal:
115+
return step + 1
116+
if nx >= 0 and nx <= 1000 and not vis[nx]:
117+
q.append((nx, step + 1))
118+
vis[nx] = True
119+
return -1
102120
```
103121

104122
### **Java**
105123

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

108126
```java
127+
class Solution {
128+
public int minimumOperations(int[] nums, int start, int goal) {
129+
IntBinaryOperator op1 = (x, y) -> x + y;
130+
IntBinaryOperator op2 = (x, y) -> x - y;
131+
IntBinaryOperator op3 = (x, y) -> x ^ y;
132+
IntBinaryOperator[] ops = { op1, op2, op3 };
133+
boolean[] vis = new boolean[1001];
134+
Queue<int[]> queue = new ArrayDeque<>();
135+
queue.offer(new int[] { start, 0 });
136+
while (!queue.isEmpty()) {
137+
int[] p = queue.poll();
138+
int x = p[0], step = p[1];
139+
for (int num : nums) {
140+
for (IntBinaryOperator op : ops) {
141+
int nx = op.applyAsInt(x, num);
142+
if (nx == goal) {
143+
return step + 1;
144+
}
145+
if (nx >= 0 && nx <= 1000 && !vis[nx]) {
146+
queue.offer(new int[] { nx, step + 1 });
147+
vis[nx] = true;
148+
}
149+
}
150+
}
151+
}
152+
return -1;
153+
}
154+
}
155+
```
156+
157+
### **C++**
158+
159+
```cpp
160+
class Solution {
161+
public:
162+
int minimumOperations(vector<int>& nums, int start, int goal) {
163+
using pii = pair<int, int>;
164+
vector<function<int(int, int)>> ops{
165+
[](int x, int y) { return x + y; },
166+
[](int x, int y) { return x - y; },
167+
[](int x, int y) { return x ^ y; },
168+
};
169+
vector<bool> vis(1001, false);
170+
queue<pii> q;
171+
q.push({start, 0});
172+
while (!q.empty()) {
173+
auto [x, step] = q.front();
174+
q.pop();
175+
for (int num : nums) {
176+
for (auto op : ops) {
177+
int nx = op(x, num);
178+
if (nx == goal) {
179+
return step + 1;
180+
}
181+
if (nx >= 0 && nx <= 1000 && !vis[nx]) {
182+
q.push({nx, step + 1});
183+
vis[nx] = true;
184+
}
185+
}
186+
}
187+
}
188+
return -1;
189+
}
190+
};
191+
```
109192
193+
### **Go**
194+
195+
```go
196+
func minimumOperations(nums []int, start int, goal int) int {
197+
type pair struct {
198+
x int
199+
step int
200+
}
201+
202+
ops := []func(int, int) int{
203+
func(x, y int) int { return x + y },
204+
func(x, y int) int { return x - y },
205+
func(x, y int) int { return x ^ y },
206+
}
207+
vis := make([]bool, 1001)
208+
q := []pair{{start, 0}}
209+
210+
for len(q) > 0 {
211+
x, step := q[0].x, q[0].step
212+
q = q[1:]
213+
for _, num := range nums {
214+
for _, op := range ops {
215+
nx := op(x, num)
216+
if nx == goal {
217+
return step + 1
218+
}
219+
if nx >= 0 && nx <= 1000 && !vis[nx] {
220+
q = append(q, pair{nx, step + 1})
221+
vis[nx] = true
222+
}
223+
}
224+
}
225+
}
226+
return -1
227+
}
110228
```
111229

112230
### **TypeScript**

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

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ We can go from 2 &rarr; 14 &rarr; 12 with the following 2 operations.
4747
<strong>Input:</strong> nums = [3,5,7], start = 0, goal = -4
4848
<strong>Output:</strong> 2
4949
<strong>Explanation:</strong>
50-
We can go from 0 &rarr; 3 &rarr; -4 with the following 2 operations.
50+
We can go from 0 &rarr; 3 &rarr; -4 with the following 2 operations.
5151
- 0 + 3 = 3
5252
- 3 - 7 = -4
5353
Note that the last operation sets x out of the range 0 &lt;= x &lt;= 1000, which is valid.
@@ -66,9 +66,9 @@ There is no way to convert 0 into 1.</pre>
6666
<pre>
6767
<strong>Input:</strong> nums = [1], start = 0, goal = 3
6868
<strong>Output:</strong> 3
69-
<strong>Explanation:</strong>
70-
We can go from 0 &rarr; 1 &rarr; 2 &rarr; 3 with the following 3 operations.
71-
- 0 + 1 = 1
69+
<strong>Explanation:</strong>
70+
We can go from 0 &rarr; 1 &rarr; 2 &rarr; 3 with the following 3 operations.
71+
- 0 + 1 = 1
7272
- 1 + 1 = 2
7373
- 2 + 1 = 3
7474
</pre>
@@ -93,13 +93,131 @@ BFS
9393
### **Python3**
9494

9595
```python
96-
96+
class Solution:
97+
def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
98+
op1 = lambda x, y: x + y
99+
op2 = lambda x, y: x - y
100+
op3 = lambda x, y: x ^ y
101+
ops = [op1, op2, op3]
102+
vis = [False] * 1001
103+
q = deque([(start, 0)])
104+
while q:
105+
x, step = q.popleft()
106+
for num in nums:
107+
for op in ops:
108+
nx = op(x, num)
109+
if nx == goal:
110+
return step + 1
111+
if nx >= 0 and nx <= 1000 and not vis[nx]:
112+
q.append((nx, step + 1))
113+
vis[nx] = True
114+
return -1
97115
```
98116

99117
### **Java**
100118

101119
```java
120+
class Solution {
121+
public int minimumOperations(int[] nums, int start, int goal) {
122+
IntBinaryOperator op1 = (x, y) -> x + y;
123+
IntBinaryOperator op2 = (x, y) -> x - y;
124+
IntBinaryOperator op3 = (x, y) -> x ^ y;
125+
IntBinaryOperator[] ops = { op1, op2, op3 };
126+
boolean[] vis = new boolean[1001];
127+
Queue<int[]> queue = new ArrayDeque<>();
128+
queue.offer(new int[] { start, 0 });
129+
while (!queue.isEmpty()) {
130+
int[] p = queue.poll();
131+
int x = p[0], step = p[1];
132+
for (int num : nums) {
133+
for (IntBinaryOperator op : ops) {
134+
int nx = op.applyAsInt(x, num);
135+
if (nx == goal) {
136+
return step + 1;
137+
}
138+
if (nx >= 0 && nx <= 1000 && !vis[nx]) {
139+
queue.offer(new int[] { nx, step + 1 });
140+
vis[nx] = true;
141+
}
142+
}
143+
}
144+
}
145+
return -1;
146+
}
147+
}
148+
```
149+
150+
### **C++**
151+
152+
```cpp
153+
class Solution {
154+
public:
155+
int minimumOperations(vector<int>& nums, int start, int goal) {
156+
using pii = pair<int, int>;
157+
vector<function<int(int, int)>> ops{
158+
[](int x, int y) { return x + y; },
159+
[](int x, int y) { return x - y; },
160+
[](int x, int y) { return x ^ y; },
161+
};
162+
vector<bool> vis(1001, false);
163+
queue<pii> q;
164+
q.push({start, 0});
165+
while (!q.empty()) {
166+
auto [x, step] = q.front();
167+
q.pop();
168+
for (int num : nums) {
169+
for (auto op : ops) {
170+
int nx = op(x, num);
171+
if (nx == goal) {
172+
return step + 1;
173+
}
174+
if (nx >= 0 && nx <= 1000 && !vis[nx]) {
175+
q.push({nx, step + 1});
176+
vis[nx] = true;
177+
}
178+
}
179+
}
180+
}
181+
return -1;
182+
}
183+
};
184+
```
102185
186+
### **Go**
187+
188+
```go
189+
func minimumOperations(nums []int, start int, goal int) int {
190+
type pair struct {
191+
x int
192+
step int
193+
}
194+
195+
ops := []func(int, int) int{
196+
func(x, y int) int { return x + y },
197+
func(x, y int) int { return x - y },
198+
func(x, y int) int { return x ^ y },
199+
}
200+
vis := make([]bool, 1001)
201+
q := []pair{{start, 0}}
202+
203+
for len(q) > 0 {
204+
x, step := q[0].x, q[0].step
205+
q = q[1:]
206+
for _, num := range nums {
207+
for _, op := range ops {
208+
nx := op(x, num)
209+
if nx == goal {
210+
return step + 1
211+
}
212+
if nx >= 0 && nx <= 1000 && !vis[nx] {
213+
q = append(q, pair{nx, step + 1})
214+
vis[nx] = true
215+
}
216+
}
217+
}
218+
}
219+
return -1
220+
}
103221
```
104222

105223
### **TypeScript**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int minimumOperations(vector<int>& nums, int start, int goal) {
4+
using pii = pair<int, int>;
5+
vector<function<int(int, int)>> ops{
6+
[](int x, int y) { return x + y; },
7+
[](int x, int y) { return x - y; },
8+
[](int x, int y) { return x ^ y; },
9+
};
10+
vector<bool> vis(1001, false);
11+
queue<pii> q;
12+
q.push({start, 0});
13+
while (!q.empty()) {
14+
auto [x, step] = q.front();
15+
q.pop();
16+
for (int num : nums) {
17+
for (auto op : ops) {
18+
int nx = op(x, num);
19+
if (nx == goal) {
20+
return step + 1;
21+
}
22+
if (nx >= 0 && nx <= 1000 && !vis[nx]) {
23+
q.push({nx, step + 1});
24+
vis[nx] = true;
25+
}
26+
}
27+
}
28+
}
29+
return -1;
30+
}
31+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func minimumOperations(nums []int, start int, goal int) int {
2+
type pair struct {
3+
x int
4+
step int
5+
}
6+
7+
ops := []func(int, int) int{
8+
func(x, y int) int { return x + y },
9+
func(x, y int) int { return x - y },
10+
func(x, y int) int { return x ^ y },
11+
}
12+
vis := make([]bool, 1001)
13+
q := []pair{{start, 0}}
14+
15+
for len(q) > 0 {
16+
x, step := q[0].x, q[0].step
17+
q = q[1:]
18+
for _, num := range nums {
19+
for _, op := range ops {
20+
nx := op(x, num)
21+
if nx == goal {
22+
return step + 1
23+
}
24+
if nx >= 0 && nx <= 1000 && !vis[nx] {
25+
q = append(q, pair{nx, step + 1})
26+
vis[nx] = true
27+
}
28+
}
29+
}
30+
}
31+
return -1
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int minimumOperations(int[] nums, int start, int goal) {
3+
IntBinaryOperator op1 = (x, y) -> x + y;
4+
IntBinaryOperator op2 = (x, y) -> x - y;
5+
IntBinaryOperator op3 = (x, y) -> x ^ y;
6+
IntBinaryOperator[] ops = { op1, op2, op3 };
7+
boolean[] vis = new boolean[1001];
8+
Queue<int[]> queue = new ArrayDeque<>();
9+
queue.offer(new int[] { start, 0 });
10+
while (!queue.isEmpty()) {
11+
int[] p = queue.poll();
12+
int x = p[0], step = p[1];
13+
for (int num : nums) {
14+
for (IntBinaryOperator op : ops) {
15+
int nx = op.applyAsInt(x, num);
16+
if (nx == goal) {
17+
return step + 1;
18+
}
19+
if (nx >= 0 && nx <= 1000 && !vis[nx]) {
20+
queue.offer(new int[] { nx, step + 1 });
21+
vis[nx] = true;
22+
}
23+
}
24+
}
25+
}
26+
return -1;
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /