A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).

Given a starting point (sx, sy) and a target point (tx, ty), return True if and only if a sequence of moves exists to transform the point (sx, sy) to (tx, ty). Otherwise, return False.

Examples:
Input: sx = 1, sy = 1, tx = 3, ty = 5
Output: True
Explanation:
One series of moves that transforms the starting point to the target is:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5) Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: False Input: sx = 1, sy = 1, tx = 1, ty = 1
Output: True 分析:因为对于起始点来讲,它后面的x和y值永远是增加的,所以,如果sx > tx 或者 sy > ty,那么明显是不行的。
但是从小到大找好像不好找,可以换种思维,从大往小找。
 class Solution {
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
if (ty < sy || tx < sx) return false;
while (tx >= sx && ty >= sy) {
if (tx > ty) tx %= ty;
else ty %= tx;
}
if (tx == sx) {
return (ty - sy) % sx == ;
}
if (ty == sy) {
return (tx - sx) % sy == ;
}
return false;
}
}

Reaching Points的更多相关文章

  1. [Swift]LeetCode780. 到达终点 | Reaching Points

    A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). Given a ...

  2. [LeetCode] Reaching Points 到达指定点

    A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). Given a ...

  3. 780. Reaching Points

    idea: 1.从后向前找 2.while (tx > ty) tx -= ty; 替为 % 操作 3.经过循环后,必定只有两种情况才true sx == tx && sy &l ...

  4. [LeetCode] 780. Reaching Points 到达指定点

    A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). Given a ...

  5. 【leetcode】Reaching Points

    题目如下: A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). G ...

  6. LeetCode 780. Reaching Points

    题目链接:https://leetcode.com/problems/reaching-points/ 题意:给定操作可以使点(x,y)变为点(x+y,y)或者点(x,x+y).现已知初始点(sx,s ...

  7. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. OI程序常见的设计陷阱

    宏定义的问题 有时候为了方便,我会大量使用宏定义.但是最近我发现下面这两个宏定义老是出问题: #define SET(x,a) memset(x,a,sizeof(x)) inline void wo ...

  2. 死磕Java之聊聊HashMap源码(基于JDK1.8)

    死磕Java之聊聊HashMap源码(基于JDK1.8) http://cmsblogs.com/?p=4731 为什么面试要问hashmap 的原理

  3. 关于kafka定期清理日志后再消费报错kafka.common.OffsetOutOfRangeException的解决

    环境: kafka  0.10 spark  2.1.0 zookeeper  3.4.5-cdh5.14.0 公司阿里云测试机,十月一放假前,没有在继续消费,假期过后回来再使用spark strea ...

  4. 记一次phoenix在不加索引的情况调优,由6s以上时间变成不到1s

    背景: 网约车预约单查询: 这里面恶心的地方是: 1个时间窗口要查询6种时间:推送订单时间(来自mongodb).有效抢单时间(来自mongodb).抢单成功时间(实时kafka).取消订单时间(实时 ...

  5. javaScript基础用Number()把其它类型转换为Number类型

    一:基本类型 字符串 把字符串转换为数字,只要字符串中包含任意一个非有效数字字符(第一个点除外)结果都是NaN,空字符串会变为数字零 console.log(Number("12.5&quo ...

  6. 如何简单的在linux上安装jdk并配置环境变量

    这篇文章是为了给我一会自己安装的时候方便使用的,所以内容很简单,平时在wendows系统上安装很容易,但是换到linux系统上面就蒙圈了. 一.下载jdk文件 我这提供的是官方的地址:http://w ...

  7. 6.使用Feign实现声明式REST调用

                        使用Feign实现声明式REST调用 6.1. Feign简介 Feign是一个声明式的REST客户端,它的目的就是让REST调用更加简单. Feign提供了H ...

  8. Django环境搭建win(二)

    Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6,17年4月4号 已经发布(长期支持版本 LTS) win7下: 一.使用pip安装 1.首先要安装pip 2.p ...

  9. ./与sh区别

    1   ./需要执行权限,使用脚本文件中第一行#!指定的shell(解释器)来执行命令(譬如常见的/bin/bash),不指定系统会调用默认shell程序 2   sh不需要执行权限,是使用sh这个s ...

  10. vue如何动态绑定v-model

    如图所示有三个字段要从弹出的输入框取值点击字段会弹出上面的弹窗,输入input会响应变化,比如点击身高,弹出输入框,输入值后身高后面会跟着一个同样的值点击体重,弹出输入框,输入值后体重后面会跟着一个同 ...