[Swift]LeetCode780. 到達終點 | Reaching Points

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-huknwvkw-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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.github

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].

從點 (x, y) 能夠轉換到 (x, x+y)  或者 (x+y, y)spa

給定一個起點 (sx, sy) 和一個終點 (tx, ty),若是經過一系列的轉換能夠從起點到達終點,則返回 True ,不然返回 Falsecode

示例:
輸入: sx = 1, sy = 1, tx = 3, ty = 5
輸出: True
解釋:
能夠經過如下一系列轉換從起點轉換到終點:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5)

輸入: sx = 1, sy = 1, tx = 2, ty = 2
輸出: False

輸入: sx = 1, sy = 1, tx = 1, ty = 1
輸出: True

注意:orm

  • sx, sy, tx, ty 是範圍在 [1, 10^9] 的整數。

Runtime: 4 ms
Memory Usage: 18.4 MB
1 class Solution {
2     func reachingPoints(_ sx: Int, _ sy: Int, _ tx: Int, _ ty: Int) -> Bool { 
3         if tx < sx || ty < sy {return false}
4         if tx == sx && (ty - sy) % sx == 0 {return true}
5         if ty == sy && (tx - sx) % sy == 0 {return true}
6         return reachingPoints(sx, sy, tx % ty, ty % tx)        
7     }
8 }

4mshtm

 1 class Solution {
 2     func reachingPoints(_ sx: Int, _ sy: Int, _ tx: Int, _ ty: Int) -> Bool {
 3         var tx = tx, ty = ty
 4         while tx >= sx && ty >= sy {
 5             if tx == ty {break}
 6             if tx > ty {
 7                 if ty > sy {
 8                     tx %= ty
 9                 } else {
10                     return (tx - sx) % ty == 0
11                 }
12             } else {
13                 if (tx > sx) {
14                     ty %= tx
15                 } else {
16                     return (ty - sy) % tx == 0
17                 }   
18             }
19         }
20         return false
21     }
22 }

8msblog

 1 class Solution {
 2     func gcd(_ a: Int, _ b: Int) -> Int {
 3         if a < b {
 4             return gcd(b, a)
 5         }
 6         if b == 0 {
 7             return a
 8         }
 9         return gcd(b, a%b)
10     }
11     
12     func reachingPoints(_ sx: Int, _ sy: Int, _ tx: Int, _ ty: Int) -> Bool {
13 
14         var tx = tx
15         var ty = ty
16         while tx >= sx && ty >= sy {
17             if tx == sx && ty == sy {
18                 return true
19             }
20             if tx > ty {
21                 var r = tx%ty
22                 if sx > r {
23                     return ty == sy && (sx-r)%ty == 0
24                 }
25                 tx = r
26             } else {
27                 var r = ty%tx
28                 if sy > r {
29                     return tx == sx && (sy-r)%tx == 0
30                 }
31                 ty = r
32             }
33         }
34         return false
35     }
36 }
相關文章
相關標籤/搜索