题目如下:

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 Note:
sx, sy, tx, ty will all be integers in the range [1, 10^9].

解题思路:看到sx,sy,tx,ty的最大值可以是10^9,那就基本上放弃穷举法了。本题比较适合倒推法,例如题目中给的例子[1,1] -> [3,5]:要想得到[3,5],那么前面的一组数组必定是[3,2] (即[3,5-3])计算得来,而[3,2]又是从[1,2] (即[3-2,2])来的,最后推出[1,1]。 原理就是用[tx,ty]中较大值减去较小值依次计算得来。所以,很快我就写出来如下代码:

while tx > 0 and ty > 0:
if ty == tx:
ret = False
break
if ty > tx:
ty -= tx
else:
tx -=ty
if sx == tx and sy == ty:
ret = True
break
return ret

可以这段代码却被[1,1,10^9,1]的输入打败。一时间我竟然想不出更好的办法,直到第二天,脑子里才出现了“用除法代替减法”的方案。方案也很简单,如果tx > ty,那么tx可以利用除法得到一个不小于max(ty,sx)的最小数。完整代码如下:

class Solution(object):
def reachingPoints(self, sx, sy, tx, ty):
"""
:type sx: int
:type sy: int
:type tx: int
:type ty: int
:rtype: bool
"""
if sx == tx and sy == ty:
return True
ret = True
while tx > 0 and ty > 0:
if ty == tx:
ret = False
break
if ty > tx:
max_y = max(tx,sy)
div = (ty - max_y) / tx
div = max (div,1)
ty -= div * tx
else:
max_x = max(ty,sx)
div = (tx - max_x) / ty
div = max (div,1)
tx -= div * ty
if sx == tx and sy == ty:
ret = True
break
return ret

【leetcode】Reaching Points的更多相关文章

  1. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  2. 【leetcode】Max Points on a Line(hard)☆

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  3. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  4. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  5. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  6. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. Linux_RHEL7_LDAP、Autofs服务

    目录 目录 前言 LDAP 加入LDAP用户认证服务器 文件自动挂载服务autofs 前言 LDAP服务器,用作于网络用户的集中管理.在企业中员工的个人帐号一般采用集中管理的方式,在不同的系统平台上也 ...

  2. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_3 Mybatis的CRUD-修改和删除操作

    增加更新操作 更新用户的配置 测试类 删除的操作 这里的parpameterType值可以是:Integer.INTEGER.int.java.lang.Integer 讲到typeAliases标签 ...

  3. 深入理解java:1.3.1 JVM内存区域的划分(运行时数据区)

    学习Java GC机制,可以帮助我们在日常工作中 排查各种内存溢出或泄露问题,解决性能瓶颈,达到更高的并发量,写出更高效的程序. 我们将从4个方面学习Java GC机制, 1,内存是如何分配的: 2, ...

  4. Intersection of Two Arrays(交集)

    来源:https://leetcode.com/problems/intersection-of-two-arrays Given two arrays, write a function to co ...

  5. 使用Oracle12c 以上的PDB创建数据库用户 密码过期的简单处理

    1. 先通过监听查看PDB的名字 Windows 打开命令行: 输入命令 lsnrctl status 一般在如图示的最下面 2. 也可以通过GS的全局配置文件来查看 数据库连接SID信息. C:\P ...

  6. MySQL-快速入门(7)索引

    1.什么是索引 索引是对数据库表中一列或者多列的值进行排序的一种结构.索引是在存储引擎中实现的,每种存储引擎中的索引不一定完全相同. MySQL中索引的存储类型有两种:btree和hash.MyISA ...

  7. Linux 最常用命令整理,建议收藏!

    Linux是目前应用最广泛的服务器操作系统,基于Unix,开源免费,由于系统的稳定性和安全性,市场占有率很高,几乎成为程序代码运行的最佳系统环境. linux不仅可以长时间的运行我们编写的程序代码,还 ...

  8. POJ 3135 Polygons on the Grid(枚举+凸包)

    题目大意是让你用这n条边放在网格上构成凸包,并且边的两端点必须在网格上. 那么比较容易想到的就是枚举可能情况,因为这样的勾股数组成情况不多,因此可以直接枚举所有连出去的边反映在坐标轴上的所有情况,最后 ...

  9. python中输入某年某月某日,判断这一天是这一年的第几天?

    输入某年某月某日,判断这一天是这一年的第几天?程序分析 特殊情况,闰年时需考虑二月多加一天: 直接上代码 #定义一个函数,判断是否为闰年 def leapyear(y): return (y % 40 ...

  10. 使用Log4Net将系统日志信息记录到记事本和数据库中

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hxpjava1/article/details/32714855 一.使用Log4Net将日志记录到 ...