|
50 | 50 |
|
51 | 51 | <!-- 这里可写通用的实现逻辑 -->
|
52 | 52 |
|
| 53 | +**方法一:逆向计算** |
| 54 | + |
| 55 | +从 `(tx, ty)` 开始逆向计算,判断是否可以到达状态 `(sx, sy)`。由于逆向计算是将 tx, ty 中的较大值减少,因此当 `tx > ty` 时可以直接将 tx 的值更新为 `tx % ty`,当 `tx < ty` 时可以将 ty 的值更新为 `ty % tx`。逆向计算需要满足 `tx > sx && ty > sy && tx != ty`。 |
| 56 | + |
| 57 | +当条件不成立时,根据此时 tx 和 ty 判断是否可以从起点转换到终点。 |
| 58 | + |
| 59 | +- 如果 `tx == sx && ty == sy`,说明此时已经到达起点状态,返回 true; |
| 60 | +- 如果 `tx == sx`,若 `ty > sy && (ty - sy) % tx == 0`,返回 true,否则返回 false; |
| 61 | +- 如果 `ty == sy`,若 `tx > sx && (tx - sx) % ty == 0`,返回 true,否则返回 false; |
| 62 | +- 如果 `tx ≠ sx && ty ≠ sy`,则不可以从起点转换到终点。 |
| 63 | + |
53 | 64 | <!-- tabs:start -->
|
54 | 65 |
|
55 | 66 | ### **Python3**
|
56 | 67 |
|
57 | 68 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 69 |
|
59 | 70 | ```python
|
60 |
| - |
| 71 | +class Solution: |
| 72 | + def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool: |
| 73 | + while tx > sx and ty > sy and tx != ty: |
| 74 | + if tx > ty: |
| 75 | + tx %= ty |
| 76 | + else: |
| 77 | + ty %= tx |
| 78 | + if tx == sx and ty == sy: |
| 79 | + return True |
| 80 | + if tx == sx: |
| 81 | + return ty > sy and (ty - sy) % tx == 0 |
| 82 | + if ty == sy: |
| 83 | + return tx > sx and (tx - sx) % ty == 0 |
| 84 | + return False |
61 | 85 | ```
|
62 | 86 |
|
63 | 87 | ### **Java**
|
64 | 88 |
|
65 | 89 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 90 |
|
67 | 91 | ```java
|
| 92 | +class Solution { |
| 93 | + public boolean reachingPoints(int sx, int sy, int tx, int ty) { |
| 94 | + while (tx > sx && ty > sy && tx != ty) { |
| 95 | + if (tx > ty) { |
| 96 | + tx %= ty; |
| 97 | + } else { |
| 98 | + ty %= tx; |
| 99 | + } |
| 100 | + } |
| 101 | + if (tx == sx && ty == sy) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + if (tx == sx) { |
| 105 | + return ty > sy && (ty - sy) % tx == 0; |
| 106 | + } |
| 107 | + if (ty == sy) { |
| 108 | + return tx > sx && (tx - sx) % ty == 0; |
| 109 | + } |
| 110 | + return false; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### **C++** |
| 116 | + |
| 117 | +```cpp |
| 118 | +class Solution { |
| 119 | +public: |
| 120 | + bool reachingPoints(int sx, int sy, int tx, int ty) { |
| 121 | + while (tx > sx && ty > sy && tx != ty) |
| 122 | + { |
| 123 | + if (tx > ty) tx %= ty; |
| 124 | + else ty %= tx; |
| 125 | + } |
| 126 | + if (tx == sx && ty == sy) return true; |
| 127 | + if (tx == sx) return ty > sy && (ty - sy) % tx == 0; |
| 128 | + if (ty == sy) return tx > sx && (tx - sx) % ty == 0; |
| 129 | + return false; |
| 130 | + } |
| 131 | +}; |
| 132 | +``` |
68 | 133 |
|
| 134 | +### **Go** |
| 135 | +
|
| 136 | +```go |
| 137 | +func reachingPoints(sx int, sy int, tx int, ty int) bool { |
| 138 | + for tx > sx && ty > sy && tx != ty { |
| 139 | + if tx > ty { |
| 140 | + tx %= ty |
| 141 | + } else { |
| 142 | + ty %= tx |
| 143 | + } |
| 144 | + } |
| 145 | + if tx == sx && ty == sy { |
| 146 | + return true |
| 147 | + } |
| 148 | + if tx == sx { |
| 149 | + return ty > sy && (ty-sy)%tx == 0 |
| 150 | + } |
| 151 | + if ty == sy { |
| 152 | + return tx > sx && (tx-sx)%ty == 0 |
| 153 | + } |
| 154 | + return false |
| 155 | +} |
69 | 156 | ```
|
70 | 157 |
|
71 | 158 | ### **...**
|
|
0 commit comments