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 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的更多相关文章
- [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 ...
- [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 ...
- 780. Reaching Points
idea: 1.从后向前找 2.while (tx > ty) tx -= ty; 替为 % 操作 3.经过循环后,必定只有两种情况才true sx == tx && sy &l ...
- [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 ...
- 【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 ...
- LeetCode 780. Reaching Points
题目链接:https://leetcode.com/problems/reaching-points/ 题意:给定操作可以使点(x,y)变为点(x+y,y)或者点(x,x+y).现已知初始点(sx,s ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Selenium报错:StaleElementReferenceException
一个学生在操作页面跳转时遇到一个Selenium报错, 如下图所示: StaleElementReferenceException: Message: stale element reference: ...
- H5页游戏内存溢出问题
记录自己解决的第一个H5页的性能问题, 关于内存溢出 拼字游戏 问题表现 初始化后, 第一次拼字并不卡. 随着拼的次数越来越多, 越来越卡 浏览器任务管理器中可以看出, 内存持续升高 确定内存问题, ...
- centos7下面ruby的升级
背景 在做redis集群时,所需要的使用ruby工具进行操作,发现在线安装的Ruby版本过低,redis支持的版本最少为2.2.2. 在线安装ruby 使用yum在线安装ruby,安装的版本为2.0. ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
- Turtle库画小猫咪
视频请点击这里哟 这个是用python库画的小猫咪,前面一步步的画整个猫的整个身体,具体步骤都写在了注释之中.身体部分画的比较快,后面画了5个心,由于画心的时候要改变好多度数,而且每次只前进一点点,所 ...
- mybatis用Map<Long,List<String>>作为参数
mapper.xml文件里的<insert id="insertByMap" parameterType="java.util.Map"> inse ...
- OUC_Summer Training_ DIV2_#13 723afternoon
A - Shaass and Oskols Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- Linux设备驱动程序 之 获取当前时间
墙上时间 内核一般通过jiffies来获取当前时间,该数值表示的是最近一次系统启动到当前的时间间隔,它和设备驱动程序无关,因为它的声明期只限于系统的运行期:但是驱动程序可以用jiffies来计算不同事 ...
- 基于DAT的中文分词方法的研究与实现
一.从Trie说起 DAT是Double Array Trie的缩写,说到DAT就必须先说一下trie是什么.Trie树是哈希树的一种,来自英文单词"Retrieval"的简写,可 ...
- arcgis 面或线要素类上的搜索游标
import arcpy infc = arcpy.GetParameterAsText(0) # Identify the geometry field # desc = arcpy.Describ ...