365. Water and Jug Problem
Water and Jug Problem
Solution
public class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if (x + y< z) {
return false;
}
if (x + y == z) {
return true;
}
int gcd = gcd(x, y);
if (gcd == 0) return false;
return z % gcd == 0;
}
private int gcd(int x, int y) {
if (y == 0) return x;
return gcd(y, x%y);
}
}Last updated