From 4b49020f3c436382af481c0c9712547dfceebc04 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 26 Mar 2020 11:33:56 +0800 Subject: [PATCH] add 365 --- README.md | 1 + src/0365-Water-and-Jug-Problem/0365.cpp | 8 ++++++++ src/0365-Water-and-Jug-Problem/0365.go | 17 +++++++++++++++++ src/0365-Water-and-Jug-Problem/0365.java | 11 +++++++++++ src/0365-Water-and-Jug-Problem/0365.js | 9 +++++++++ src/0365-Water-and-Jug-Problem/0365.py | 7 +++++++ src/addProb.py | 8 ++++---- 7 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/0365-Water-and-Jug-Problem/0365.cpp create mode 100644 src/0365-Water-and-Jug-Problem/0365.go create mode 100644 src/0365-Water-and-Jug-Problem/0365.java create mode 100644 src/0365-Water-and-Jug-Problem/0365.js create mode 100644 src/0365-Water-and-Jug-Problem/0365.py diff --git a/README.md b/README.md index 2bf986ba..7a3edaef 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,7 @@ LeetCode |0349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | c | [c++](./src/0349-Intersection-of-Two-Arrays/0349.cpp) |[python](./src/0349-Intersection-of-Two-Arrays/0349.py)||| |Easy| |0350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | c | [c++](./src/0350-Intersection-of-Two-Arrays-II/0350.cpp) |[python](./src/0350-Intersection-of-Two-Arrays-II/0350.py)||| |Easy| |0354|[Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/) | c | [c++](./src/0354-Russian-Doll-Envelopes/0354.cpp) |[python](./src/0354-Russian-Doll-Envelopes/0354.py)||| |Hard| +|0365|[Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem/)|c|[c++](./src/0365-Water-and-Jug-Problem/0365.cpp)|[python](./src/0365-Water-and-Jug-Problem/0365.py)|[go](./src/0365-Water-and-Jug-Problem/0365.go)|[js](./src/0365-Water-and-Jug-Problem/0365.js)|[java](./src/0365-Water-and-Jug-Problem/0365.java)|Medium| |0371|[Sum of Two Integers](https://leetcode.com/problems/russian-doll-envelopes/) | c | [c++](./src/0371-Sum-of-Two-Integers/0371.cpp) |[python](./src/0371-Sum-of-Two-Integers/0371.py)||| |Easy| |0377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | c | [c++](./src/0377-Combination-Sum-IV/0377.cpp) |[python](./src/0377-Combination-Sum-IV/0377.py)||| |Medium| |0389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | c | [c++](./src/0389-Find-the-Difference/0389.cpp) |[python](./src/0389-Find-the-Difference/0389.py)||| |Easy| diff --git a/src/0365-Water-and-Jug-Problem/0365.cpp b/src/0365-Water-and-Jug-Problem/0365.cpp new file mode 100644 index 00000000..3b19e719 --- /dev/null +++ b/src/0365-Water-and-Jug-Problem/0365.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + bool canMeasureWater(int x, int y, int z) { + if (x + y < z) return false; + if (x == 0 || y == 0) return z == 0 || x + y == z; + return z % gcd(x, y) == 0; + } +}; \ No newline at end of file diff --git a/src/0365-Water-and-Jug-Problem/0365.go b/src/0365-Water-and-Jug-Problem/0365.go new file mode 100644 index 00000000..27a17a57 --- /dev/null +++ b/src/0365-Water-and-Jug-Problem/0365.go @@ -0,0 +1,17 @@ +func canMeasureWater(x int, y int, z int) bool { + if x + y < z { + return false + } + + if x == 0 || y == 0 { + return z == 0 || x + y == z + } + return z % gcd(x, y) == 0 +} + +func gcd(a, b int) int { + if b == 0 { + return a + } + return gcd(b, a % b) +} \ No newline at end of file diff --git a/src/0365-Water-and-Jug-Problem/0365.java b/src/0365-Water-and-Jug-Problem/0365.java new file mode 100644 index 00000000..feaca79b --- /dev/null +++ b/src/0365-Water-and-Jug-Problem/0365.java @@ -0,0 +1,11 @@ +class Solution { + public boolean canMeasureWater(int x, int y, int z) { + if (x + y < z) return false; + if (x == 0 || y == 0) return z == 0 || x + y == z; + return z % gcd(x, y) == 0; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } +} \ No newline at end of file diff --git a/src/0365-Water-and-Jug-Problem/0365.js b/src/0365-Water-and-Jug-Problem/0365.js new file mode 100644 index 00000000..bb5ad86a --- /dev/null +++ b/src/0365-Water-and-Jug-Problem/0365.js @@ -0,0 +1,9 @@ +var canMeasureWater = function(x, y, z) { + let gcd = function(a, b) { + return b == 0 ? a : gcd(b, a % b); + } + + if (x + y < z) return false; + if (x == 0 || y == 0) return z == 0 || x + y == z; + return z % gcd(x, y) == 0; +}; \ No newline at end of file diff --git a/src/0365-Water-and-Jug-Problem/0365.py b/src/0365-Water-and-Jug-Problem/0365.py new file mode 100644 index 00000000..053da2ff --- /dev/null +++ b/src/0365-Water-and-Jug-Problem/0365.py @@ -0,0 +1,7 @@ +class Solution: + def canMeasureWater(self, x: int, y: int, z: int) -> bool: + if x + y < z: + return False + if x == 0 or y == 0: + return z == 0 or x + y == z + return z % math.gcd(x, y) == 0 \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index b8521c68..18c5cb2d 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -2,10 +2,10 @@ import os, bisect # 题目名称 -name = "Longest Happy Prefix" -ID = 1392 -url = "https://leetcode.com/problems/longest-happy-prefix/" -difficult = "Hard" +name = "Water and Jug Problem" +ID = 365 +url = "https://leetcode.com/problems/water-and-jug-problem/" +difficult = "Medium" prog = ['c', 'cpp', 'py', 'go', 'js', 'java']