Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise.
The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).
Example 1:
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)
Example 2:
Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: false
Example 3:
Input: sx = 1, sy = 1, tx = 1, ty = 1
Output: true
Constraints:
1 <= sx, sy, tx, ty <= 109
class Solution {
public:
bool reachingPoints(int sx, int sy, int tx, int ty) {
if(sx==tx&&sy==ty) return true;
if(sx>tx||sy>ty) return false;
int diff=tx-ty;
if(diff<0) diff*=-1;
if(diff<sx&&diff<sy) return false;
while(tx>sx&&ty>sy) {
diff=tx-ty;
if(diff<0) diff*=-1;
if(diff<sx&&diff<sy) return false;
//加上上面这段会快很多
if(tx<ty){
printf("*\n");
ty=ty%tx;
}
else{
printf("**\n");
tx=tx%ty;
}
}
printf("%d,%d\n",tx,ty);
// diff=tx-ty;
// if(diff<0) diff*=-1;
// if(diff<sx||diff<sy) return false;
if(tx==sx&&ty==sy) return true;
else if(tx<sx||ty<sy) return false;
else if(tx==sx){
printf("#\n");
if((ty-sy)%sx!=0||(ty-sy)<sx) return false;
}
else if(ty==sy){
printf("##\n");
if((tx-sx)%sy!=0||(tx-sx)<sy) return false;
}
// else if(sx)
return true;
}
};
刚开始骗分,之后涨分时有的思路
确实思路可能不会在一开始明确,但总比没有思路强
该博客探讨了一种基于特定操作(x, y -> x, x+y 或 x+y, y)将坐标(sx, sy)转换为坐标(tx, ty)的方法。通过示例解释了如何通过这些操作判断是否能从起点到达终点,并提供了一个C++实现的解决方案。文章强调了在进行坐标转换时的条件检查和优化技巧。

230

被折叠的 条评论
为什么被折叠?



