题目考察的知识点:双指针
题目解答方法的文字分析:遍历数组,符合地区则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; } };