# [128\. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) ```cpp= class Solution { public: int longestConsecutive(vector<int>& nums) { unordered_set<int> st(nums.begin(), nums.end()); int res = 0; for(auto& num : nums) { // 如果是連續數字的話,避免重複計算 if(st.count(num - 1)) continue; int cur = num; int length = 1; while(st.count(cur + 1)) { ++cur; ++length; } res = max(res, length); } return res; } }; ``` :::success - 時間複雜度:$O(N)$ - 空間複雜度:$O(N)$ :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up