365. 水壶问题

有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?

如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水。

你允许:

装满任意一个水壶

清空任意一个水壶

从一个水壶向另外一个水壶倒水,直到装满或者倒空

示例 1: (From the famous “Die Hard” example)

输入: x = 3, y = 5, z = 4

输出: True

示例 2:

输入: x = 2, y = 6, z = 5

输出: False

PS:

如果z=ax+by(a,b均整),x与y最大公约数为g,那么z一定是g的整数倍,即z%g=0!!

class Solution {
public boolean canMeasureWater(int x, int y, int z) {
return z == 0 || (x + y >= z && z % gcd(x, y) == 0);
}
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
} }

Java实现 LeetCode 365 水壶问题的更多相关文章

  1. Leetcode 365.水壶问题

    水壶问题 有两个容量分别为 x升和 y升的水壶以及无限多的水.请判断能否通过使用这两个水壶,从而可以得到恰好 z升的水? 如果可以,最后请用以上水壶中的一或两个来盛放取得的 z升 水. 你允许: 装满 ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. u-boot spl 学习总结

    什么是SPL? SPL(secondary program loader)是一个十分小的bin文件,它是用来引导主u-boot文件.对于一些SRAM很小的SOC,无法一次性加载ROM中的bootloa ...

  2. gather函数

    gather(input, dim, index):根据  index,在  dim  维度上选取数据,输出的  size  与  index  一致 # input (Tensor) – 源张量 # ...

  3. 基于 abp vNext 和 .NET Core 开发博客项目

    项目介绍 此个人博客项目底层基于 ABP Framework (不完全依赖)搭建项目 和免费开源跨平台的 .NET Core 3.1 开发,可作为 .NET Core 入门项目进行学习,支持各种主流数 ...

  4. 解决Hystrix dashboard Turbine 一直 Loading…… 及其他坑

    问题一.请求 /hystrix.stream 报错,我这里以端口9001为例 请求 http://localhost:9001/hystrix.stream 报404 是因为Srping Boot 2 ...

  5. bootstrap基本页面

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  6. 盲注fuzz

    \'"%df'%df"and%201=1and%201=2'%20and%20'1'='1'%20and%20'1'='2"%20and%20"1"= ...

  7. abp(net core)+easyui+efcore实现仓储管理系统——入库管理之十二(四十八)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  8. clickhouse基本操作一

    常用SQL 创建表 1 2 3 4 5 6 7 CREATE TABLE b6logs( eventDate Date, impid UInt64, uid String, idfa String, ...

  9. 5.5 Go defer

    5.5 Go defer 程序开发中经常要创建资源(数据库初始化连接,文件句柄,锁等),在程序执行完毕都必须得释放资源,Go提供了defer(延时机制)更方便.更及时的释放资源. 1.内置关键字def ...

  10. mysql运维入门5:MySQL+kepalived高可用架构

    keepalive 类似3/4/7层交换机制的软件,也就是平时说的第三层.第四层.第七层交换 作用是检测web服务器的状态,如果有一台web服务器.mysql服务器宕机.或工作出现故障,keepali ...