题目如下:

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. 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_9 typeAliases标签和package标签

    配置别名 上面制定了好了别名后,映射文件就可以简写了.不区分大小写 皆可以小写也可以大写 大小写混着也可以 测试 package 直接把com.itheima.domain下面所有的类都注册了 可以使 ...

  2. 测开之路一百四十九:jinja2模板之宏

    jinja2是python的模板引擎, 在写python web使用的过程中,macro可以节省大量的代码 比如上一篇的渲染页面 <!DOCTYPE html><html lang= ...

  3. C 语言的函数 - 内存分析

    函数基本概念 Linux 中,函数在内存的代码段(code 区),地址比较靠前. 函数定义 C 语言中,函数有三个要素:入参.返回值.函数名,缺一不可.函数使用前必须先声明,或者在使用之前定义. 函数 ...

  4. 操作系统(5)实验0——makefile的写法

    之前GCC那部分我提到过,gcc啥啥啥啥傻傻的那个指令只能够编译简单的代码,如果要干大事(例如突然心血来潮写个c开头的神经网络库之类的),还是要写Makefile来编译.其实在Windows下通常用I ...

  5. POJ 1330 Nearest Common Ancestors (dfs+ST在线算法)

    详细讲解见:https://blog.csdn.net/liangzhaoyang1/article/details/52549822 zz:https://www.cnblogs.com/kuang ...

  6. tensorflow学习之Saver保存读取

    目前不是很懂..但主要意思是tf可以把一开始定义的参数,包括Weights和Biases保存到本地,然后再定义一个变量框架去加载(restore)这个参数,作为变量本身的参数进行后续的训练,具体如下: ...

  7. php php-fpm、nginx和js

    1 php-fpm是什么 php-fpm是php fastCGI process manager的缩写.它是php的进程管理器,对每个请求的处理都是一个进程. php-fpm管理了一个进程池,假如进程 ...

  8. zookeeper设置客户端连接超时被expired

    在网络环境非常差的情况下,使用zookeeper集群往往会遇到连接expired了: 客户端提示连接从ZOO_CONNECTION_STATE变为ZOO_EXPIRED_SEESION_STATE,然 ...

  9. kafka 安装教程

    安装详述: https://www.jianshu.com/p/596f107e901a 3.0:运行:cd 到: D:\Installed_software\Professional\kafka_2 ...

  10. c++构造顺序

    1. 静态成员最先构造,按照静态成员初始化顺序,不是类里面的声明顺序 2. 父类构造 3. 非静态成员构造,按照类成员声明顺序,不是逗号初始化成员顺序 4. 自身构造函数 Demo: class Te ...