【leetcode刷题笔记】Longest Consecutive Sequence

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.



题解:首先构建一个hashmap,键值为num中的所有元素,这样查找一个元素是否在num中就只需要O(1)的时间了。

接下来以上面给定的例子来说明算法:

比如上述num数组中第一个元素是100,那么就找101或者99在不在num中,发现不在;

num中第二个元素是4,那么就找3或者5是否在num中,发现3在num中,而5不在;于是继续找2在不在num中,然后找1在不在num中.....最终知道有4,3,2,1这么一个长度为4的序列。在这个遍历过程中,因为已经找到了序列4,3,2,1,所以如果以后遍历到3的时候再找到的序列3,2,1已经没有意义了(肯定比当前的最长序列短),所以3,2,1这三个元素在未来的遍历中,我们都不需要考虑了,这里就把它们在hashmap中对应的值改成1,作为标志。

num中第三个元素是200,那么就找199或者201是否在num中,发现都不在;

num中还有元素1,3,2,基于上述描述的原因,就不再遍历了。

这样就保证了每个元素最多遍历一次,时间复杂度为O(n)。

示例图如下;

代码如下:

 1 public class Solution {
 2     public int longestConsecutive(int[] num) {
 3         int answer = 0;
 4         HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
 5
 6         for(int i:num)
 7             map.put(i, 0);
 8
 9         for(int i:num){
10             if(map.get(i) == 1)
11                 continue;
12
13             int currMax = 1;
14             int tmp = i-1;
15             //left
16             while(map.containsKey(tmp)){
17                 map.put(tmp, 1); //we have visited tmp,so we don‘t start with tmp in the future
18                 currMax++;
19                 tmp--;
20             }
21
22             tmp = i+1;
23             while(map.containsKey(tmp)){
24                 map.put(tmp, 1);
25                 currMax++;
26                 tmp++;
27             }
28
29             if(currMax > answer)
30                 answer = currMax;
31         }
32         return answer;
33     }
34 }

【leetcode刷题笔记】Longest Consecutive Sequence,布布扣,bubuko.com

时间: 2024-12-25 18:42:32

【leetcode刷题笔记】Longest Consecutive Sequence的相关文章

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: 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 al

【leetcode刷题笔记】Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer. 代码如下: 1 public class Solution { 2 public String longestCommonPrefix

【leetcode刷题笔记】Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

【leetcode刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

【leetcode刷题笔记】Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode刷题笔记】Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

【leetcode刷题笔记】Count and Say

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an