class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int res = 0;
int count = 0;
unordered_map<int, int> hashTable;
for(int i = 0; i < nums.size(); i++){
auto item = hashTable.find(nums[i]);
if(item == hashTable.end()){
hashTable[nums[i]] = count;
}
}
for(int i = 0; i < nums.size(); i++){
auto item = hashTable.find(nums[i] - 1);
count = 1;
if(item == hashTable.end()){
while(hashTable.find(nums[i] + count) != hashTable.end()){
count++;
}
}
if(count > res)
res = count;
}
return res;
}
};
LeetCode Hot100 128.最长连续序列
于 2024-08-03 11:27:01 首次发布