解题思路:倒水问题,转换为两个杯子向一个大杯子中倒水、舀水的问题,即mx+ny=d,若z<=x+y且z%d==0,说明存在m、n使得最终剩余水为z。根据贝祖等式,d为x、y的最大公约数。
class Solution(object):
def canMeasureWater(self, x, y, z):
"""
:type x: int
:type y: int
:type z: int
:rtype: bool
"""
if z==0:
return True
if z > x+y:
return False
return z%self.gcd(x,y)==0
def gcd(self,x,y):
if y == 0:
return x
else:
return self.gcd(y,x%y)