leetcode之longest-consecutive-sequence

來源:互聯網
上載者:User

問題描述:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
Given[100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.

Your algorithm should run in O(n) complexity.

題目要求演算法時間複雜度是O(n),所以先排序的思想是行不通的,這裡為了做比較,也實現了先排序的方法。
另一種演算法的思想就是用空間換時間:
採用hash表,先初始化一個hash表, 儲存所有數組元素, 然後遍曆這個數組, 對找到的數組元素, 去搜尋其相連的上下兩個元素是否在hash表中, 如果在, 刪除相應元素並增加此次尋找的資料長度, 如果不在, 從下一個元素出發尋找。

package suda.alex.leetcode;import java.util.Arrays;import java.util.HashSet;import java.util.Scanner;import java.util.Set;public class LongestConsecutive {    /**     * @param args     */    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("input the size of num:");        int len = scanner.nextInt();        System.out.println("input num:");        int[] num = new int[len];        for (int i = 0; i < len; i++) {            num[i] = scanner.nextInt();        }        System.out.println("the longestConsecutive num is:"                + longestConsecutive2(num));    }    public static int longestConsecutive1(int[] num) {        int len = num.length;        if (len == 0) {            return 0;        }        Arrays.sort(num);        int count = 1;        int maxLen = 1;        for (int i = 1; i < len; i++) {            if (num[i] == num[i - 1] + 1) {                count++;            } else {                if (num[i] != num[i - 1]) {                    count = 1;                }            }            if (count > maxLen) {                maxLen = count;            }        }        return maxLen;    }    public static int longestConsecutive2(int[] num) {        int len = num.length;        if (len == 0) {            return 0;        }        Set<Integer> set = new HashSet<Integer>();        int maxLen = 1;        for (int i = 0; i < len; i++) {            set.add(num[i]);        }        for (int i = 0; i < len; i++) {            int count = 1;            int leftNum = num[i] - 1;            int rightNum = num[i] + 1;            while (set.contains(leftNum)) {                count++;                set.remove(leftNum);                leftNum--;            }            while (set.contains(rightNum)) {                count++;                set.remove(rightNum);                rightNum++;            }            if(count > maxLen){                maxLen = count;            }        }        return maxLen;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.