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 ...
随机推荐
- windows上SVN服务器以及客户端TortoiseSVN的安装配置
(1)svn的安装 1. 下载软件:Setup-Subversion-1.7.5.msi,安装就很容易了,一路NEXT 2. 把SVN的bin文件夹路径添加到环境变量中 把svn安装目录下的bin路径 ...
- php文件锁(转)
bool flock ( int handle, int operation [, int &wouldblock] );flock() 操作的 handle 必须是一个已经打开的文件指针.o ...
- winform C#屏幕右下角弹出消息框并自动消失
①弹出信息框后慢慢下降消失 在主窗体中新增按钮重命名为btnShowAndDisappearMessages,在click事件中写如下代码: private void btnShowAndDisapp ...
- angular 控制器之间值得传递
<div ng-controller="ParentCtrl"> <!--父级--> <div ng-controller="SelfCtr ...
- supervisor进程管理
install : apt-get install supervisor crete a xxxx.conf file at /etc/supervisor/conf.d the cont ...
- Administration Commands
Commands useful for administrators of a hadoop cluster. balancer Runs a cluster balancing utility. A ...
- linux制作文件系统
1.获取文件系统源码并解压 这里使用的源码是天嵌提供的“root_qtopia_2.2.0_2.6.30.4_20100601.tar.bz2” #tar xvf root_qtopia_2..0_2 ...
- Javascript AMD模块化规范-备用
AMD是"Asynchronous Module Definition"的缩写,意思是"异步模块定义". 模块定义define(id?, dependencie ...
- Android Learning:多线程与异步消息处理机制
在最近学习Android项目源码的过程中,遇到了很多多线程以及异步消息处理的机制.由于之前对这块的知识只是浅尝辄止,并没有系统的理解.但是工程中反复出现让我意识到这个知识的重要性.所以我整理出这篇博客 ...
- java项目创建和部署
http://www.cnblogs.com/nexiyi/archive/2012/12/28/2837560.html http://dead-knight.iteye.com/blog/1841 ...