题目如下:

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. Python Module_oslo.vmware_连接 vCenter

    目录 目录 前言 Install oslsvmware How to use the vSphere Web Service SDK 前言 oslo.vmware 是一个由 Python 实现的 vC ...

  2. Maven使用WEB-INF/lib下面的jar编译和打包

    在某些情况下,maven无法下载依赖的jar,或者依赖的m2会非常的大,上G那是随随便便的事.为了方便修改和编译,在打出的war包基础上,或者直接把tomcat的webapps下的项目拿出来,就可以用 ...

  3. oracle-只读数据文件的备份与恢复

    11 只读数据文件的备份与恢复 只读数据文件是只读表空间的数据文件,数据块包括文件头在内部允许更改 SQL> alter tablespace yhqt read only; SQL> a ...

  4. 【HANA系列】SAP HANA数据处理的理解与分析一

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA数据处理的理解与 ...

  5. 【Python】我的豆瓣短评爬虫的多线程改写

    对之前我的那个豆瓣的短评的爬虫,进行了一下架构性的改动.尽可能实现了模块的分离.但是总是感觉不完美.暂时也没心情折腾了. 同时也添加了多线程的实现.具体过程见下. 改动 独立出来的部分: MakeOp ...

  6. Window7系统安装和使用MySql

    win7系统MySql安装和使用教程 首先下载mysql安装包 点击下载mysql v5.7.1 解压 下载完毕后解压在D盘 路径为D:\mysql-5.7.13-winx64,然后进入这个目录,新建 ...

  7. [转帖]同事推荐的的aira2

    Windows系统安装最新版Aria2客户端及使用教程 https://www.moerats.com/archives/519/ 改天学习一下. 说明:之前都是说的在Linux VPS服务器上安装A ...

  8. [转帖]解决K8S 安装只有 一直提示:kernel:unregister_netdevice: waiting for eth0 to become free. Usage count = 1 的方法

    Centos7 终端报Message from syslogd :kernel:unregister_netdevice https://www.jianshu.com/p/96d7e2cd9e99 ...

  9. Java 多线程编程之:notify 和 wait 用法

    wait 和 notify 简介 wait 和 notify 均为 Object 的方法: Object.wait() —— 暂停一个线程 Object.notify() —— 唤醒一个线程 从以上的 ...

  10. linux的进程间通信概述

    一 进程间通信 1.1. linux内核提供多种进程间通信机制 a. 无名管道和有名管道 b. SystemV IPC:信号量.消息队列.共享内存 c. Socket域套接字 d. 信号 1.2. 无 ...