Water and Jug Problem思路:能否合法取决于2个条件:1.两个容器的和是否大于等于目标值,2.目标值是否能被2个容器的最大公约数整除
GitHub地址:https://github.com/corpsepiges/leetcode
public class Solution {
public boolean canMeasureWater(int x, int y, int z) {
return z==x||z==y||(z<=x+y&&z%gcd(x, y)==0);
}
public int gcd(int x,int y){
return x==0?y:gcd(y%x,x);
}
}