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

每次用大的减小的,然后差值和小的再减,减减减减减减到差值=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. 配置hive元数据库mysql时候出现 Unable to find the JDBC database jar on host : master

    解决办法: cd /usr/share/java/,(没有java文件夹,自行创建)rz  mysql-connector-java-***.jar,mv mysql-connector-java-* ...

  2. LBP特征提取实现

    捯饬了一两天才搞好! 在lbp.m下输入下面代码,运行结果如图: 代码: I=imread('rice.png'); mapping=getmapping(8,'u2'); H1=lbp(I,1,8, ...

  3. sql语句select group by order by where一般先后顺序 转载

    写的顺序:select ... from... where.... group by... having... order by..执行顺序:from... where...group by... h ...

  4. 去掉Visual Studio 编辑器里中文注释的红色波浪线 转载

    我们通常用visual studio进行开发的时候,我们通常会用到一款比较流行比较方便的插件,那就是Visual Assist X,它可以增强Microsoft开发环境下的编辑能力,支持C/C++,C ...

  5. 接触.net5年了,感觉自己的知识面很狭隘。

    08年毕业找工作期间开始接触网页开发,由于在学校了混了4年时间,我只能从html标记语言开始学习,后来应聘到一个网站建设公司,开始学习ps.Dreamweaver和asp.由于基础薄弱,一个月后离开了 ...

  6. C#程序中:如何启用进程、结束进程、查找进程

    在启动某个程序之前,如果需要先检查改程序是否已经运行,可以查找进程里有没有这个进程,再根据查找进程后的结果进行相应的判断操作. 产找进程的范围是任务管理器中的进程列表.如果进程被隐藏了,结果……(我没 ...

  7. jquery 属性与css操作

    属性1.属性    1.1 attr(name|properties|key,value|key,fn)        1) 获取属性值    $("img").attr(&quo ...

  8. mysql慢速查询

    linux下配置慢查询: 修改my.cnf文件,在[mysqld]模块下添加 #slow_query_log=1 有些人说这个是slow_query的开关,但是我加上以后提示错误.log_slow_q ...

  9. [python]文本处理1.2

    1.0初步完成了文本截取需要信息的处理 1.1 修复了格式所造成的遗漏字符 1.2 去除了遗漏字符中的多余字符 bug-文本test14 有遗漏字符 bug-修复的遗漏字符中含有\n 未被识别为换行符

  10. python重要的函数代码块

    注意:现在python3.3.5中print打印语句有了新写法: 1. python指定生成随机数 >>> import random >>> >>&g ...