yokostan / Leetcode-Solutions

Doing exercise on Leetcode. Carry on!
0 stars 3 forks source link

Leetcode #365. Water and Jug Problem #281

Open yokostan opened 5 years ago

yokostan commented 5 years ago
class Solution {
    public boolean canMeasureWater(int x, int y, int z) {
        if (x == z || y == z || x + y == z) return true;
        if (x + y < z) return false;
        return z % GCD(x, y) == 0;
    }

    public int GCD(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}

reference: https://leetcode.com/problems/water-and-jug-problem/discuss/83715/Math-solution-Java-solution