LeetCode-Water and Jug Problem
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs.
If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end.
Operations allowed:
- Fill any of the jugs completely with water.
- Empty any of the jugs.
- Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.
Example 1: (From the famous "Die Hard" example)
Input: x = 3, y = 5, z = 4
Output: True
Example 2:
Input: x = 2, y = 6, z = 5
Output: False
Credits:
Special thanks to @vinod23 for adding this problem and creating all test cases.
Analysis:
It is a number theory problem.
https://discuss.leetcode.com/topic/49238/math-solution-java-solution/2
Solution:
public class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if (x+y < z) return false; if (x==z || y==z || x+y==z) return true; return z%GCD(x,y)==0; } public int GCD(int x, int y){
while (x%y!=0){
int temp = x;
x = y;
y = temp%y;
}
return y;
}
}
LeetCode-Water and Jug Problem的更多相关文章
- [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 ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- 【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 ...
- Leetcode 365. Water and Jug Problem
可以想象有一个无限大的水罐,如果我们有两个杯子x和y,那么原来的问题等价于是否可以通过往里面注入或倒出水从而剩下z. z =? m*x + n*y 如果等式成立,那么z%gcd(x,y) == 0. ...
- [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 ...
- 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 ...
- 365. Water and Jug Problem量杯灌水问题
[抄题]: 简而言之:只能对 杯子中全部的水/容量-杯子中全部的水进行操作 You are given two jugs with capacities x and y litres. There i ...
- 365 Water and Jug Problem 水壶问题
有两个容量分别为 x升 和 y升 的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水.你允许: 装满任 ...
- 365. Water and Jug Problem
莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...
随机推荐
- 了解C#文件操作
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- XML文件标签名一致,而属性值不同,如何遍历取值写法 摘录
<EssentialFunctions> <Qualification description="We Offer" source="AdDe ...
- AutoFac文档5(转载)
目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 扫描 autofac可 ...
- vue组件调用(用npm安装)
vue用webpack打包方式新建项目,注意刚开始可以先关闭路由和代码错误检测功能 1.建立了一个Hi.vue的组件 <template> <div>Hi~~{{msg}}-- ...
- REPEAT_BYTE(x)宏
位置:include/linux/kernel.h 定义: #define REPEAT_BYTE(x) ((~0ul / 0xff)*(x)) 作用:结果看下面,作用未知,好像是为了一个叫:word ...
- .atitit.web 推送实现解决方案集合(3)----dwr3 Reverse Ajax
.atitit.web 推送实现解决方案集合(3)----dwr3 Reverse Ajax 1. 原理实现 1 2. Page 增加配置,增加回调函数dwr.engine.setActiveRev ...
- AppModify修改app.config
public class AppModify { /// <summary> /// 依据连接串名字connectionName返回数据连接字符串 /// </summary> ...
- Centos7 安装Git-cola
首先安装Git sudo yum -y install git* 找到 git-all.noarch , 安装这个. sudo yum install git-all.noarch ========= ...
- windows phone 应用提交商店失败总结
应用完成后,在提交微软商店时,可能因为各种各样的问题导致提交审核失败.以前的审核失败并没有总结,希望今后 把各种提交审核失败的情况总结一下,以减少今后提交认证时浪费时间. 1.商店的屏幕截图上不能包含 ...
- js 静态方法 静态变量 实例方法 实例变量
1.静态方法的定义 Js代码 var BaseClass = function() {}; // var BaseClass=new Function(); BaseClass.f1 = func ...