【发布时间】:2021-05-08 17:24:12
【问题描述】:
当我的 nums 向量为 [1,3,5,7,9,11,13] 时,为什么输出为 13 11 9 7 5 3 5 7 9 11 13 12 10 8 6 4。 我认为它应该是 1 3 5 7 9 11 13 的组合之一。
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int,int> m;
for(auto e:nums){
m[e]++;
}
int maxLen=0;
for(auto it=m.begin();it!=m.end();it++)
{
int ele=it->first;
cout<<ele<<" ";
if(m[ele-1])
maxLen=max(maxLen,(m[ele]+m[ele-1]));
if(m[ele+1])
maxLen=max(maxLen,(m[ele]+m[ele+1]));
}
return maxLen;
}
};
【问题讨论】:
标签: c++ stl unordered-map