题解 | #连续的牛群标签序列#
连续的牛群标签序列
https://www.nowcoder.com/practice/5db36ae74c274176a0cf9274e9f9ed3e
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tag int整型vector
* @return int整型
*/
int longestConsecutive(vector<int>& tag) {
// write code here
set<int> u_s(tag.begin(),tag.end());
vector<int> v(u_s.begin(),u_s.end());
// 测试
// for(auto num:v)
// cout << num << " ";
// cout << endl;
// 滑动窗口
int left = 0,right = 0;
int len = 0;
for(; right<v.size();++right)
{
if(v[right]-v[left]!=right-left)
{
len = max(len, right-left);
left = right;
--right;
}
}
len = max(len, right-left);
return len;
}
};
查看15道真题和解析