-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWaterAndJugProblem.java
More file actions
85 lines (77 loc) · 2.4 KB
/
WaterAndJugProblem.java
File metadata and controls
85 lines (77 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package math;
// Source : https://leetcode.com/problems/water-and-jug-problem/
// Id : 365
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2020-11-05
// Topic : Math, BFS
// Level : Medium +
// Other :
// Tips :
// Result : 100.00% 13.93%
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class WaterAndJugProblem {
public boolean canMeasureWater(int x, int y, int z) {
if (z > x + y) {
return false;
}
Set<Integer> set = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
queue.offer(0);
while (!queue.isEmpty()) {
int tmp = queue.poll();
/**
* The add() method of Set in Java is used to add a specific element into a Set collection.
* The function adds the element only if the specified element is not already present in the set
* else the function return False if the element is already present in the Set.
*/
if (tmp + x <= x + y && set.add(tmp + x)) {
queue.offer(tmp + x);
}
if (tmp + y <= x + y && set.add(tmp + y)) {
queue.offer(tmp + y);
}
if (tmp - x >= 0 && set.add(tmp - x)) {
queue.offer(tmp - x);
}
if (tmp - y >= 0 && set.add(tmp - y)) {
queue.offer(tmp - y);
}
if (set.contains(z)) {
return true;
}
}
return false;
}
/**
* Bézout's identity.
* If you don't know, now you know.
* It is highly unlikely you can figure this out the first time.
*
* @param x
* @param y
* @param z
* @return
*/
public boolean canMeasureWater2(int x, int y, int z) {
//public boolean canMeasureWater(int x, int y, int z) {
if (z > y + x)
return false;
// assume x is not smaller
if (x < y)
return canMeasureWater(y, x, z);
if (y == 0)
return z == 0 || z == x;
return z % gcb(x, y) == 0;
}
// m >= n
private int gcb(int m, int n) {
if (m % n == 0) {// 若余数为0,返回最大公约数
return n;
} else { // 否则,进行递归,把n赋给m,把余数赋给n
return gcb(n, m % n);
}
}
}