/*****Leetcode_128_LongestConsecutiveSequence_Hard*****/ /** * Leetcode_128_LongestConsecutiveSequence_最长连续序列_Hard * 难度:Hard * <p> * 题目介绍: * Given an unsorted array of integers, find the length of the longest consecutive elements sequence. * Your algorithm should run in O(n) complexity. * Example: * Input: [100, 4, 200, 1, 3, 2] * Output: 4 * Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. * 思路分析: * 方法1: 先排序然后统计找出最长子序列的长度;排序复杂度为O(N·logN) * 方法2:利用HashMap实现O(N)时间复杂度。 * https://leetcode.com/problems/longest-consecutive-sequence/discuss/41055/My-really-simple-Java-O(n)-solution-Accepted * 利用HashMap解决。 * We will use HashMap. The key thing is to keep track of the sequence length and store that * in the boundary points of the sequence. * <p> * Whenever a new element n is inserted into the map, do two things: * 1. See if n - 1 and n + 1 exist in the map, and if so, it means there is an existing sequence next to n. * Variables left and right will be the length of those two sequences, while 0 means there is no sequence * and n will be the boundary point later. Store (left + right + 1) as the associated value to key n into the map. * 2. Use left and right to locate the other end of the sequences to the left and right of n respectively, * and replace the value with the new length. * Everything inside the for loop is O(1) so the total time is O(n). * Please comment if you see something wrong. Thanks. * * 关键点:保证已经构成子序列的两端点对应的value为所在子序列的长度,如2,3,5,6,7,8; * get(2)和get(3)都返回2,get(5)和get(8)都返回4,对于6和7对应的value不做要求。 * 每当向HashMap中插入一个元素时,需要做两件事情: * 1. 判断hashMap中是否已经存在num-1和num+1,如果存在,则意味着在num两边存在子序列, * 分别获取对应的左边和右边序列的长度,如果不存在则默认为0; * 2. 更新新序列的长度。updateSeqLen = leftSeqLen + rightSeqLen + 1; * 需要向hashMap中put三个键值对:num、num-leftSeqLen、num+rightSeqLen,其value都是updateSeqLen。 * num-leftSeqLen对应的是左子序列的左端点;num+rightSeqLen对应的是右子序列的右端点。 */ /** * 方法1:先排序然后统计找出最长子序列的长度 */ public int longestConsecutive2(int[] nums) { if (nums.length == 0 || nums == null) return 0; Arrays.sort(nums);//排序 int maxLen = 1, count = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] == nums[i - 1]) continue;//防重处理 else if (nums[i] == nums[i - 1] + 1) count++;//连续 else { if (count > maxLen) maxLen = count;//更新最大长度 count = 1; } } return maxLen > count ? maxLen : count; } /** * 方法2:利用HashMap实现 */ public int longestConsecutive(int[] nums) { if (nums == null || nums.length == 0) return 0; HashMap<Integer, Integer> hashMap = new HashMap<>(); int updateSeqLen, maxLen = 0; for (int num : nums) { if (hashMap.containsKey(num)) continue; int leftSeqLen = hashMap.containsKey(num - 1) ? hashMap.get(num - 1) : 0; int rightSeqLen = hashMap.containsKey(num + 1) ? hashMap.get(num + 1) : 0; updateSeqLen = leftSeqLen + rightSeqLen + 1; hashMap.put(num, updateSeqLen); hashMap.put(num - leftSeqLen, updateSeqLen); hashMap.put(num + rightSeqLen, updateSeqLen); if (updateSeqLen > maxLen) maxLen = updateSeqLen; } return maxLen; }
[LeetCode] Longest Consecutive Sequence 求最长连续序列
最新推荐文章于 2020-12-10 04:41:41 发布