780. Reaching Points

时间:2023-12-13 15:34:50

idea:

1.从后向前找

2.while (tx > ty) tx -= ty; 替为 % 操作

3.经过循环后,必定只有两种情况才true

  1. sx == tx && sy <= ty && (ty - sy) % sx == 0
  2. sy == ty && sx <= tx && (tx - sx) % sy == 0
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
while (tx > sx && ty > sy){
if (tx > ty) tx %= ty;
else ty %= tx;
}
return sx == tx && sy <= ty && (ty - sy) % sx == 0 ||
sy == ty && sx <= tx && (tx - sx) % sy == 0; }