首页 > 其他 > 详细

water-and-jug-problem

时间:2016-06-27 15:37:10      阅读:244      评论:0      收藏:0      [点我收藏+]

以下这个解法也是参考了一些讨论:

https://leetcode.com/discuss/110235/c-solution-using-euclidean-algorithm

还有这个解释原理的,没有看懂:https://leetcode.com/discuss/110525/a-little-explanation-on-gcd-method

 

class Solution {
    int gcd(int a, int b) {
        // below suppose a <= b
        // and if b < a, then b%a == b
        return a==0?b:gcd(b%a, a);
    }
public:
    bool canMeasureWater(int x, int y, int z) {
        // notice, operator priority: % > == > && > ||
        // so below is equivalent to
        // (z == 0) || ((z <= (x+y)) && ((z % gcd(x,y)) == 0))
        return z == 0 || z <= (x+y) && z % gcd(x, y) == 0;
    }
};
31 / 31 test cases passed.
Status: 

Accepted

Runtime: 0 ms

water-and-jug-problem

原文:http://www.cnblogs.com/charlesblc/p/5620143.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!