780. Reaching Points

idea:
1.從後向前找
2.while (tx > ty) tx -= ty; 替爲 % 操做
3.通過循環後,一定只有兩種狀況才truejava

  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;
            
    }
相關文章
相關標籤/搜索