莫名奇妙找了个奇怪的规律。

每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止。(较小的数 和 差值 相等为止,这么说更确切)

然后看能不能整除就行了。

有些特殊情况。

看答案是用GCD做的,2行就写出来了,但是不是很理解。

Ax + By = z,A B为正负 int,就是有接。。神奇。

放个自己写的垃圾代码。

public class Solution {
public boolean canMeasureWater(int x, int y, int z)
{ if(z>x+y) return false;
if(z == 0) return true;
if(x == y) return (z==x) || (z == 2*x);
if(x == 0 || y == 0) return (z == x) || (z == y);
if(z%x == 0 || z % y == 0) return true; int large = Math.max(x,y);
int small = Math.min(x,y);
int diff = large - small; while(diff != small)
{
large = Math.max(diff,small);
small = Math.min(diff,small);
diff = large - small;
} return z%diff == 0;
}
}

GCD的两行纯复制粘贴,不是很明白:

public boolean gcd(int a, int b)
{
return b == 0? a:gcd(b,a%b);
}
public boolean canMeasureWater(int x, int y, int z)
{
return ( x + y > z && z % gcd(x,y) == 0) || x + y == z;
}

365. Water and Jug Problem的更多相关文章

  1. 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 ...

  2. 【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 ...

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

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

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

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

  5. Leetcode 365. Water and Jug Problem

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

  6. 365 Water and Jug Problem 水壶问题

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

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. java_设计模式_迭代器模式_Iterator Pattern(2016-08-12)

    迭代子(Iterator)模式又叫游标(Cursor)模式,是对象的行为模式. 定义:提供一种方法访问一个容器对象中各个元素,而又不暴露该对象的内部细节. 类型:行为类模式 类图: 如果要问java中 ...

  2. 修改hosts使用谷歌服务

    原文链接如下: http://www.findspace.name/res/72#_1

  3. [Linux]ubuntu安装ftp服务器

     1: 安装vsftpd~$ sudo apt-get install vsftpd  or~$ yum install vsftpd温馨提示:ubuntu10.10自己装了,这步省略. 2: 配置v ...

  4. excel快递单号查询工具以及源码

    Function kdcx(kd, orderid) Dim Err, url, kdtime, link, Errcode, Status Select Case kd '此处支持的快递公司很多的 ...

  5. 那些年优秀的HTML5活动页面

    一个好的手机活动宣传 更能让人分享 传播是爆炸性的 下面是我平时看到一些好的微信活动宣传页面  分享给大家 其中用到的技术 常做微信活动 专题页面的人 可以看看大神们是怎么做的  这样到自己做的时候 ...

  6. mysql备份,还原命令

    mysql导出数据1.导出整个数据库mysqldump -u用户名 -p 数据库名 >备份文件2.导出一个表mysqldump -u用户名 -p databaseName tablename & ...

  7. PHPCMS实现文章置顶功能的方法

    我个人喜欢把PHPCMS当作博客来用,而作为一个博客,怎能少了文章置顶功能呢?其中用PHPCMS实现置顶功能非常简单,无非是修改下推荐位的名称为置顶,然后在文章列表中推送需要置顶的文章罢了. 不过博客 ...

  8. 【python】闰年规则

    公历闰年判定遵循的规律为: 四年一闰,百年不闰,四百年再闰. 公历闰年的简单计算方法(符合以下条件之一的年份即为闰年)1.能被4整除而不能被100整除.2.能被400整除.

  9. dubbo 负载均衡中策略决策

    在dubbo中的服务端负载均衡配置,如果像以下情况,将需要决策最终的负载策略问题: <dubbo:application name="hello-world-server" ...

  10. java web 代码

    原 30套JSP网站源代码合集 IT小白白 发布时间: 2012/12/28 14:30 阅读: 272 收藏: 3 点赞: 0 评论: 0 JSP技术是以Java语言作为脚本语言的,JSP网页为整个 ...