题解 | #连续的牛群标签序列#
连续的牛群标签序列
https://www.nowcoder.com/practice/5db36ae74c274176a0cf9274e9f9ed3e
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tag int整型vector
* @return int整型
*/
int longestConsecutive(vector<int>& tag)
{
// write code here
sort(tag.begin(),tag.end());
int result = 0;
for (int i = 0;i < tag.size();++i)
{
int cnt = 1;
for (int j = i;j < tag.size();++j)
{
if (tag[j + 1] - tag[j] == 1)
{
cnt += 1;
}
else
{
break;
}
}
result = max(result,cnt);
}
return result;
}
};
我实在想不到咋用哈希解题
