water-and-jug-problem
Posted 笨鸟居士的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了water-and-jug-problem相关的知识,希望对你有一定的参考价值。
以下这个解法也是参考了一些讨论:
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的主要内容,如果未能解决你的问题,请参考以下文章