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

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==?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 == || z <= (x+y) && z % gcd(x, y) == ;
}
};
31 / 31 test cases passed.
Status:

Accepted

Runtime: 0 ms

water-and-jug-problem的更多相关文章

  1. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  2. Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  3. 【leetcode】365. Water and Jug Problem

    题目描述: You are given two jugs with capacities x and y litres. There is an infinite amount of water su ...

  4. [Swift]LeetCode365. 水壶问题 | Water and Jug Problem

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  5. 365. Water and Jug Problem量杯灌水问题

    [抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...

  6. 365. Water and Jug Problem (GCD or BFS) TBC

    https://leetcode.com/problems/water-and-jug-problem/description/ -- 365 There are two methods to sol ...

  7. 【LeetCode】365. Water and Jug Problem 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...

  8. Leetcode 365. Water and Jug Problem

    可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...

  9. 365. Water and Jug Problem

    莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...

  10. 365 Water and Jug Problem 水壶问题

    有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许:    装满任 ...

随机推荐

  1. 《Android源码设计模式》--享元模式

    No1: 享元模式是对象池的一种实现.享元模式用来尽可能减少内存使用量,它适合用于可能存在大量重复对象的场景,来缓存可共享的对象,达到对象共享.避免创建过多对象的效果,这样一来就可以提升性能.避免内存 ...

  2. supervisor安装(sentos7)

    其实现在网络上supervisor的教程有很多,比较杂,我找了几个对我来说是有帮助的教程,再结合自己的理解做一些笔记,可以供自己以后翻看. 链接:https://www.cnblogs.com/Hai ...

  3. Adobe Photoshop CC 2017-18.0安装教程

    Adobe Photoshop CC 2017-18.0安装教程 注:下载链接在文章后面 第一步:首先请将电脑的网络断开,很简单:禁用本地连接或者拔掉网线,这样就可以免除登录Creative Clou ...

  4. Java 集合之 Map

    Map 就是另一个顶级接口了,总感觉 Map 是 Collection 的子接口呢.Map 主要用于表示那些含有映射关系的数据,存储的是一组一组的键值对.Map 是允许你将某些对象与其它一些对象关联起 ...

  5. Python闭包Closure 2

    由于Python中,变量作用域为LEGB,所以在函数内部可以读取外部变量,但是在函数外不能读取函数内的变量.但是出于种种原因,我们需要读取函数内的变量时候怎么办?那就是在函数内在加一个函数. def ...

  6. 1025 PAT Ranking (25)(25 point(s))

    problem Programming Ability Test (PAT) is organized by the College of Computer Science and Technolog ...

  7. BZOJ2111 ZJOI2010排列计数

    根据Pi>Pi/2可以看出来这是一个二叉树 所以我们可以用树形DP的思想 f[i]=f[i<<1]*f[i<<1|1]*C(s[i]-1,s[i<<1]),s ...

  8. Linux怎么开启ssh

    一.查看ssh开启状态 service ssh status 这是已经开启了的状态 二.如果没有开启  键入以下命令开启 service ssh start 三.开启后如果不能利用xshell远程访问 ...

  9. [BZOJ5109]大吉大利,晚上吃鸡!

    [BZOJ5109]大吉大利,晚上吃鸡! 题目大意: 一张\(n(n\le5\times10^4)\)个点\(m(m\le5\times10^4)\)条边的无向图,节点编号为\(1\)到\(n\),边 ...

  10. Vue-router浅识

    一.router-link及router-view :用来做导航,通过传入to属性来指定链接 :用来做路由出口,路由匹配到的组件都会渲染在这里 const router = new VueRouter ...