题解 | #寻找最合适的生育区域#

寻找最合适的生育区域

https://www.nowcoder.com/practice/c183c254a5c94b9da341fb27fb3caf99

题目考察的知识点:双指针

题目解答方法的文字分析:遍历数组,符合地区则count++,否则count=1,重新计算;每次更新max。

本题解析所用的编程语言:c++

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param heights int整型vector 
     * @param k int整型 
     * @return int整型
     */
    int findMaxRangeWithinThreshold(vector<int>& heights, int k)
    {
        // write code here
        int count = 1;
        int max = 0;
        for (int i = 0; i < heights.size() - 1; ++i)
        {
            if (abs(heights[i + 1] - heights[i]) < k)
                ++count;
            else
                count = 1;

            if (max < count)
                max = count;
        }
        return max;
    }
};

全部评论

相关推荐

码农索隆:传音老登来也。 但是这个我不知道怎么回答,不仅仅传音吧,很多公司在候选人不第一时间接受offer或主动将报道时间延期时,都会再从池子里面捞人,直到l捞到满足公司所有要求的人。
秋招的第一个offer,...
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务