[LeetCode 双周赛22] 1. 两个数组间的距离值(暴力、常规解法)

1. 题目来源

链接:5348. 两个数组间的距离值

2. 题目说明

在这里插入图片描述
在这里插入图片描述

3. 题目解析

方法一:暴力+常规解法

题意很明确,数据范围正常,暴力枚举即可。拼手速的一道题。

参见代码如下:

// 执行用时 :48 ms, 在所有 C++ 提交中击败了100.00%的用户
// 内存消耗 :8.3 MB, 在所有 C++ 提交中击败了100.00%的用户

class Solution {
public:
    int res = 0;
    int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
        for (auto& e1 : arr1) {
            int flag = 0;
            for (auto& e2 : arr2) {
                if (abs(e1 - e2) <= d) flag = 1;
            }
            if (flag == 0) ++res;
        }
        return res;
    }
};
发布了391 篇原创文章 · 获赞 329 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/yl_puyu/article/details/105036752