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 ...
随机推荐
- js小分享
之前实现一些js代码时,总觉得无法下手,所以最近在学习一下特别细的知识点,分享笔记.嘻嘻,偷个小懒,我把自己的笔记拍个照片就不打字了.嘎嘎,放心放心,自觉得字写的还算ok的啦- 表示家里的老弟玩游戏, ...
- GoJS研究,简单图表制作。
话不多说,先上图 我在这个中加入了缩略图.鼠标放大缩小等功能. <!doctype html> <html> <head> <title>Flowcha ...
- oracle删除用户所有表
在删除数据表的时候往往遇到外键约束无法删除的情况,我们可以通过以下几步将数据库表删除,建议在删除库之前先对数据库进行备份,养成良好习惯. 1.删除外键 --查询用户所有表的外键,owner条件为use ...
- 利用jquery进行ajax提交表单和附带的数据
1.获取表单数据: $form.serialize() 2.附带数据:input[status]=1 3.构造url链接:url = $form.attr('action') + '?input[st ...
- 关闭linux终端命令行退格报警声(centos7亲测有效)
首先这个声音不是外置音频设备发出,而是主板上的蜂鸣器 1,使用root账户登录 2,vi 打开 ~/.bashrc 脚本 3,在脚本的最后一行加上 setterm -blength 0 4,保存脚本 ...
- js常用字符串函数
// JS字符串 //1.replace字符串替换,只能换第一部分,就是说多个字符相同,只能换下最先的 var str='helloworld!'; alert(str.replace('llo',' ...
- HttpWebRequest 写入报错
HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size ...
- [我的疑问]String? = "Skiy Chan" 中的问号是什么意思?
var optionalName : String? = "Skiy Chan" String? = "Skiy Chan" 中的问号是什么意思?目前还在看ap ...
- Use_Case
What is Use-Case 2.0?Use Case: A use case is all the ways of using a system to achieve a particular ...
- SpringSecurity数据库中存储用户、角色、资源
这几天项目中用到了SpringSecurity做登陆安全.所以在这写一下也许可以帮助一下其他人,自己也熟悉一下 SpringSecurity配置文件如下: <beans:beans xmlns= ...