[LeetCode] Longest Consecutive Sequence 求最長連續序列


 

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.

 

這道題要求求最長連續序列,並給定了O(n)復雜度限制,我們的思路是,使用一個集合HashSet存入所有的數字,然后遍歷數組中的每個數字,如果其在集合中存在,那么將其移除,然后分別用兩個變量pre和next算出其前一個數跟后一個數,然后在集合中循環查找,如果pre在集合中,那么將pre移除集合,然后pre再自減1,直至pre不在集合之中,對next采用同樣的方法,那么next-pre-1就是當前數字的最長連續序列,更新res即可。這里再說下,為啥當檢測某數字在集合中存在當時候,都要移除數字。這是為了避免大量的重復計算,就拿題目中的例子來說吧,我們在遍歷到4的時候,會向下遍歷3,2,1,如果都不移除數字的話,遍歷到1的時候,還會遍歷2,3,4。同樣,遍歷到3的時候,向上遍歷4,向下遍歷2,1,等等等。如果數組中有大量的連續數字的話,那么就有大量的重復計算,十分的不高效,所以我們要從HashSet中移除數字,代碼如下:

 

C++ 解法一:

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        int res = 0;
        unordered_set<int> s(nums.begin(), nums.end());
        for (int val : nums) {
            if (!s.count(val)) continue;
            s.erase(val);
            int pre = val - 1, next = val + 1;
            while (s.count(pre)) s.erase(pre--);
            while (s.count(next)) s.erase(next++);
            res = max(res, next - pre - 1);
        }
        return res;
    }
};

 

Java 解法一:

public class Solution {
    public int longestConsecutive(int[] nums) {
        int res = 0;
        Set<Integer> s = new HashSet<Integer>();
        for (int num : nums) s.add(num);
        for (int num : nums) {
            if (s.remove(num)) {
                int pre = num - 1, next = num + 1;
                while (s.remove(pre)) --pre;
                while (s.remove(next)) ++next;
                res = Math.max(res, next - pre - 1);
            }
        }
        return res;
    }
}

 

我們也可以采用哈希表來做,剛開始HashMap為空,然后遍歷所有數字,如果該數字不在HashMap中,那么我們分別看其左右兩個數字是否在HashMap中,如果在,則返回其哈希表中映射值,若不在,則返回0,雖然我們直接從HashMap中取不存在的映射值,也能取到0,但是一旦去取了,就會自動生成一個為0的映射,那么我們這里再for循環的開頭判斷如果存在映射就跳過的話,就會出錯。然后我們將left+right+1作為當前數字的映射,並更新res結果,同時更新num-left和num-right的映射值。

下面來解釋一下為啥要判斷如何存在映射的時候要跳過,這是因為一旦某個數字創建映射了,說明該數字已經被處理過了,那么其周圍的數字很可能也已經建立好了映射了,如果再遇到之前處理過的數字,再取相鄰數字的映射值累加的話,會出錯。舉個例子,比如數組 [1, 2, 0, 1],當0執行完以后,HashMap中的映射為 {1->2, 2->3, 0->3},可以看出此時0和2的映射值都已經為3了,那么如果最后一個1還按照原來的方法處理,隨后得到結果就是7,明顯不合題意。還有就是,之前說的,為了避免訪問不存在的映射值時,自動創建映射,我們使用m.count() 先來檢測一下,只有存在映射,我們才從中取值,否則就直接賦值為0,參見代碼如下:

 

C++ 解法二:

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        int res = 0;
        unordered_map<int, int> m;
        for (int num : nums) {
            if (m.count(num)) continue;
            int left = m.count(num - 1) ? m[num - 1] : 0;
            int right = m.count(num + 1) ? m[num + 1] : 0;
            int sum = left + right + 1;
            m[num] = sum;
            res = max(res, sum);
            m[num - left] = sum;
            m[num + right] = sum;
        }
        return res;
    }
};

 

Java 解法二:

public class Solution {
    public int longestConsecutive(int[] nums) {
        int res = 0;
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        for (int num : nums) {
            if (m.containsKey(num)) continue;
            int left = m.containsKey(num - 1) ? m.get(num - 1) : 0;
            int right = m.containsKey(num + 1) ? m.get(num + 1) : 0;
            int sum = left + right + 1;
            m.put(num, sum);
            res = Math.max(res, sum);
            m.put(num - left, sum);
            m.put(num + right, sum);
        }
        return res;
    }
}

 

類似題目:

Binary Tree Longest Consecutive Sequence

 

參考資料:

https://leetcode.com/problems/longest-consecutive-sequence/

https://leetcode.com/problems/longest-consecutive-sequence/discuss/41055/my-really-simple-java-on-solution-accepted

https://leetcode.com/problems/longest-consecutive-sequence/discuss/41060/a-simple-csolution-using-unordered_setand-simple-consideration-about-this-problem

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM