双周赛第一题|刷题打卡

115 阅读1分钟

一、题目描述:

二、思路分析:

周赛第一题向来是手速题,为了找到有效点,需要for循环内部进行if判断,为了找到最近的有效点,需要时刻判断min_index 和min_dis;

三、AC 代码:

class Solution {
public:
    int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
        int ret  = -1;
        int t = -1;
        for(int i = 0; i < points.size(); i ++){
            if(x == points[i][0] || y == points[i][1]){
                if(ret == -1){
                    ret = abs(points[i][0] - x) + abs(points[i][1] - y);
                    t = i;
                }
                else{
                    int a = abs(points[i][0] - x) + abs(points[i][1] - y);
                    if(a < ret){
                        ret = a;
                        t = i;
                    }
                }
            }
        }
        return t;
    }
};

四、总结:

手速题没啥总结的,手速再快一点读题再快一点就好

本文正在参与「掘金 2021 春招闯关活动」, 点击查看 活动详情