LeetCode开心刷题五十六天——128. Longest Consecutive Sequence

最近刷题进展尚可,但是形式变化了下,因为感觉眼睛会看瞎,所以好多写在纸上。本来想放到文件夹存储起来,但是太容易丢了,明天整理下,赶紧拍上来把

今晚是周末,这一周都在不停的学学学,我想下周怕是不能睡午觉了,中午回去床对我的诱惑太大了,我得想办法,一进门先把被褥收起来,再放个欢快的歌,中午少吃点,加油小可爱

之前欠下的烂帐,把太多简单题做完,导致剩下的都是难题,所以万万记住一点捷径都不要走

128看花花酱大神的解法,发现对hashtable的了解十分不足,甚至一些常见函数都不知道是干什么的

这道题涉及的知识点竟然还有并查集,逃来逃去没逃出去。

128. Longest Consecutive Sequence

Hard

2416138FavoriteShare

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.

BONUS:auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推断,可以大大简化我们的编程工作。下面是一些使用auto的例子。
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_map<int,int> h;
        int ans=0;

        for(int num:nums){
            if(h.count(num)) continue;
            //check whether can find iterator
            auto it_l=h.find(num-1);
            auto it_r=h.find(num+1);

            int l=it_l!=h.end()?it_l->second:0;
            int r=it_r!=h.end()?it_r->second:0;
            int t=l+r+1;

            h[num]=h[num-l]=h[num+r]=t;

            ans=max(ans,t);
        }
        return ans;
    }
};

原文地址:https://www.cnblogs.com/Marigolci/p/12046855.html

时间: 2024-08-26 04:58:29

LeetCode开心刷题五十六天——128. Longest Consecutive Sequence的相关文章

LeetCode开心刷题五十五天——117. Populating Next Right Pointers in Each Node II

问题亟待解决: 1.一个问题一直困扰着我,想看下别人是怎么处理树的输入的,最好是以层级遍历这种清楚直观的方式. 2.关于指针*的使用 因此也导致代码不完整,没有主函数对Solution类的调用 117. Populating Next Right Pointers in Each Node II Medium 1161165FavoriteShare Given a binary tree struct Node { int val; Node *left; Node *right; Node

LeetCode开心刷题二十六天——49.Group Anagrams

49. Group Anagrams Medium 1824116FavoriteShare Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat

LeetCode开心刷题五十一天——118. Pascal&#39;s Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal&#39;s Triangle II

118. Pascal's Triangle Easy 87984FavoriteShare Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1],

LeetCode开心刷题二十九天——63. Unique Paths II**

63. Unique Paths II Medium 938145FavoriteShare A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

124. Longest Consecutive Sequence/128. Longest Consecutive Sequence 本题难度: Medium/Hard Topic: Data Structure Description Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Example Given [100, 4, 200, 1,

128. Longest Consecutive Sequence(js)

128. 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 longe

LeetCode开心刷题十三天——24

习惯就是人生的最大指导 ——休谟 24. Swap Nodes in Pairs Medium 1209107FavoriteShare Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed. 防止用增加值,存储前后变量只进行值交换,而不处理

Java for LeetCode 128 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 i

128. 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 r