题解 | #寻找最合适的生育区域#
寻找最合适的生育区域
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;
}
};

查看22道真题和解析