最长连续序列
128. 最长连续序列
给定一个未排序的整数数组
nums
,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为 O(n)
的算法解决此问题。
示例 1:
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
示例 2:
输入:nums = [0,3,7,2,5,8,4,6,0,1] 输出:9
提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
解法一:先升序排列并去重后,判定左右连续的值是否存在,如果存在则连续元素数量加1,否则重新开始计算连续数量
1 #include <iostream> 2 #include <unordered_map> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 7 class Solution { 8 public: 9 int longestConsecutive(vector<int>& nums) { 10 // 先对数组进行升序排序 11 std::sort(nums.begin(), nums.end()); 12 // 对于数组中重复的元素只保留第一个(去重) 13 std::vector<int>::iterator it; 14 it = std::unique(nums.begin(), nums.end()); 15 nums.resize(std::distance(nums.begin(), it)); 16 17 int size = nums.size(); 18 if (size == 0) { 19 return 0; 20 } 21 22 int maxLen = 1; 23 int tmpMax = 1; 24 for (int i = 0; i < size - 1; i++) { 25 // 如果当前值加1等于后面值,则表示连续,连续元素个数加1并计算最大连续元素个数, 26 // 否则最大连续元素个数重新计算 27 if (nums[i + 1] == nums[i] + 1) { 28 tmpMax++; 29 maxLen = std::max(maxLen, tmpMax); 30 } else { 31 tmpMax = 1; 32 } 33 } 34 return maxLen; 35 } 36 }; 37 38 int main() 39 { 40 Solution *test = new Solution(); 41 42 std::vector<int> vec = {100, 100, 200, 1, 2, 2, 2, 3, 3, 7, 9, 10}; 43 std::cout << test->longestConsecutive(vec) << endl; // 3 44 45 vec = {}; 46 std::cout << test->longestConsecutive(vec) << endl; // 0 47 48 vec = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1}; 49 std::cout << test->longestConsecutive(vec) << endl; // 9 50 51 vec = {100, 4, 200, 1, 3, 2}; 52 std::cout << test->longestConsecutive(vec) << endl; // 4 53 54 delete test; 55 system("pause"); 56 return 0; 57 }
解法二:采用散列表进行存储左右连续元素,最大连续元素个数为左右间隔元素个数
1 #include <iostream> 2 #include <unordered_map> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 7 class Solution { 8 public: 9 int longestConsecutive(vector<int>& nums) { 10 unordered_map<int, int> hashMap; // 存储当前值所在连续区间的左右端点,key->左, value->右或者key->右, value->左 11 int maxLen = 0; 12 for (const auto ¤tVal : nums) { 13 if (hashMap.count(currentVal) > 0) { 14 continue; 15 } 16 int left = currentVal; 17 int right = currentVal; 18 hashMap[currentVal] = currentVal; 19 // 如果当前值所在连续区间的左端点存在,则更新左端点 20 if (hashMap.count(currentVal - 1) != 0) { 21 left = hashMap[currentVal - 1]; 22 } 23 // 如果当前值所在连续区间的右端点存在,则更新右端点 24 if (hashMap.count(currentVal + 1) != 0) { 25 right = hashMap[currentVal + 1]; 26 } 27 hashMap[left] = right; 28 hashMap[right] = left; 29 maxLen = std::max(maxLen, right - left + 1); 30 } 31 return maxLen; 32 } 33 }; 34 35 int main() 36 { 37 Solution *test = new Solution(); 38 39 std::vector<int> vec = {100, 100, 200, 1, 2, 2, 2, 3, 3, 7, 9, 10}; 40 std::cout << test->longestConsecutive(vec) << endl; // 3 41 42 vec = {}; 43 std::cout << test->longestConsecutive(vec) << endl; // 0 44 45 vec = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1}; 46 std::cout << test->longestConsecutive(vec) << endl; // 9 47 48 vec = {100, 4, 200, 1, 3, 2}; 49 std::cout << test->longestConsecutive(vec) << endl; // 4 50 51 delete test; 52 system("pause"); 53 return 0; 54 }
运行效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix