365. Water and Jug Problem
莫名奇妙找了个奇怪的规律。
每次用大的减小的,然后差值和小的再减,减减减减减减到差值=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的更多相关文章
- 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 ...
- 【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 ...
- 365. Water and Jug Problem量杯灌水问题
[抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- Leetcode 365. Water and Jug Problem
可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...
- 365 Water and Jug Problem 水壶问题
有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许: 装满任 ...
- [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 ...
- 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 ...
- [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 Winform 异步更新控件
刚才看到有人问为了winfrom中,在大数据绑定的时候出现画面假死的状态,为了解决这个问题希望通过再开一个线程来给控件绑定数据,可是画面还是会假死.现在看到的方法有1.掩耳盗铃法(Control.Ch ...
- 使用Eclipse把java文件打包成jar 含有第三方jar库的jar包
使用Eclipse把java文件打包成jar 含有第三方jar库的jar包 网上打包说用eclipse安装fat jar插件,但是貌似现在都不能用了,所以我只能按照eclipse自带的方法打包了. ...
- 关于java中for和foreach循环
for循环中的循环条件中的变量只求一次值!具体看最后的图片 foreach语句是java5新增,在遍历数组.集合的时候,foreach拥有不错的性能. foreach是for语句的简化,但是forea ...
- 实现基于Memcache存储的Session类
自主实现Session功能的类,基于文件方式存储Session数据,测试基本通过,还比较好玩,实际应用没有意义,只不过是学习Session是如何实现的. 使用基于文件的Session存取瓶颈可能都是在 ...
- 防范DDOS攻击脚本
防范DDOS攻击脚本 #防止SYN攻击 轻量级预防 iptables -N syn-flood iptables -A INPUT -p tcp --syn -j syn-flood iptables ...
- Less小记
Less除了在引用的时候link和script有顺序之外,在编译过程中,less中的代码顺序也会造成对样式的重置.
- HTML 5结构
进行总体布局时候,具体可以用的方法. 1.大纲:文档中各内容区块的结构编排. 内容区块可以使用标题元素来展示各级内容区块的标题. 关于内容区块的编排可以分为“显示编排”和“隐式编排”. 显示编排:明确 ...
- button 垂直分布
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型 button.frame = CGRectMak ...
- JS贪吃蛇游戏
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】
题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...