leetcode 780. Reaching Points

该博客探讨了一种基于特定操作(x, y -> x, x+y 或 x+y, y)将坐标(sx, sy)转换为坐标(tx, ty)的方法。通过示例解释了如何通过这些操作判断是否能从起点到达终点,并提供了一个C++实现的解决方案。文章强调了在进行坐标转换时的条件检查和优化技巧。

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;
    }
};

刚开始骗分,之后涨分时有的思路
确实思路可能不会在一开始明确,但总比没有思路强

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值