# Solution of mine

• I think my code is not elegant, can you give me some advice to make it better?

``````struct llist{
int tail;
};
class Solution {
public:
int longestConsecutive(vector<int> &num) {

if (num.size() == 0 || num.size() == 1)
{
return num.size();
}
unordered_map<int, int> M;
vector<llist> L;
llist temp;
int iTemp;
for (auto & p : num)
{
if (M.count(p) == 0)
{
if (M.count(p - 1) != 0 && M.count(p + 1) != 0)
{
iTemp = L[M[p + 1]].tail;
L[M[p - 1]].tail = L[M[p + 1]].tail;
M[iTemp] = M[p - 1];
M.insert(unordered_map<int, int>::value_type(p, M[p - 1]));
}
else if (M.count(p - 1) != 0)
{
L[M[p - 1]].tail = p;
M.insert(unordered_map<int, int>::value_type(p, M[p - 1]));
}
else if (M.count(p + 1) != 0)
{
M.insert(unordered_map<int, int>::value_type(p, M[p + 1]));
}
else
{
L.push_back(temp);
M.insert(unordered_map<int, int>::value_type(p, L.size() - 1));
}
}
}
int maxL = 0;
for (auto &p : L)
{
if(maxL < p.tail - p.head + 1)
maxL = p.tail - p.head + 1;
}
return maxL;
}
``````

};

• ur solution is good enough. however, u can record the 'max' in dynamic programming without vector<llist>. besides, the 'if else bla bla' is a little bit complicate, u can try to simplify it.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.