class Solution {
public:
bool canMeasureWater(int x, int y, int target) {
if(x + y < target){
return false;
}
if(!x || !y){
return !target || x + y == target;
}
return target % gcd(x, y) == 0; //ax+by=target 有解当且仅当 target 是 x,y 的最大公约数的倍数
}
};
LeetCode 365. 水壶问题
最新推荐文章于 2024-08-20 17:50:28 发布