https://leetcode.com/problems/reaching-points/description/
题目大意:询问 (sx,sy) 能否通过 (sx+sy,sy) 或者(sx,sx+sy)变到(tx,ty)。
解题思路:注意到变化的方式只有加法,数据范围为[1,1e9]所以,从后往前递推,当tx>ty的时候,只可能是由 (tx-ty,ty)变来的。注意处理一下 相等的情况。
class Solution { public boolean reachingPoints(int sx, int sy, int tx, int ty) { if(sx==tx && sy==ty) return true; while(true) { if(sx==tx && sy==ty) break; // System.out.println(tx+" "+ty); if(sx>tx || sy>ty) return false; if(sx==tx) { if((ty-sy) % sx==0) return true; else return false; } if(sy==ty) { if((tx-sx) % sy==0) return true; else return false; } if(tx!=ty) { if(tx>ty) tx-=ty; else ty-=tx; } else { return false; } } return true; } }