本文主要是介绍Java | Leetcode Java题解之第365题水壶问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {public boolean canMeasureWater(int x, int y, int z) {if (x + y < z) {return false;}if (x == 0 || y == 0) {return z == 0 || x + y == z;}return z % gcd(x, y) == 0;}public int gcd(int x, int y) {int remainder = x % y;while (remainder != 0) {x = y;y = remainder;remainder = x % y;}return y;}
}
这篇关于Java | Leetcode Java题解之第365题水壶问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!