数组 Longest Consecutive Sequence

本文介绍了一种使用哈希表寻找最长连续整数序列的算法。通过遍历输入数组并在哈希表中标记出现过的元素,该算法能在O(n)的时间复杂度内找到最长连续序列的长度。

思想:

序列无序且要求O(n),哈希最有可能。

线性的找一个可以向两边扩张最大的元素,返回扩张的长度。


class Solution {
public:
    int longestConsecutive(vector<int> &num) {
        unordered_map<int, bool> used;
        
        for(auto i: num)
            used[i] = false;
        
        int longest = 0;
        
        for(auto i: num) {
            if(used[i])
                continue;
            int length = 1;
            used[i] = true;
            for(int j = i + 1; used.find(j) != used.end(); j++) {
                used[j] = true;
                length++;
            }
            for(int j = i - 1; used.find(j) != used.end(); j--) {
                used[j] = true;
                length++;
            }
            longest = max(longest, length);
        }
        return longest;
    }
};


std::unordered_map

template < class Key,                                    // unordered_map::key_type
           class T,                                      // unordered_map::mapped_type
           class Hash = hash<Key>,                       // unordered_map::hasher
           class Pred = equal_to<Key>,                   // unordered_map::key_equal
           class Alloc = allocator< pair<const Key,T> >  // unordered_map::allocator_type
           > class unordered_map;
Unordered Map
Unordered maps are associative containers that store elements formed by the combination of a  key value and a mapped value, and which allows for fast retrieval of individual elements based on their keys.

In an  unordered_map, the  key value is generally used to uniquely identify the element, while the  mapped value is an object with the content associated to this  key. Types of  key and  mapped value may differ.

Internally, the elements in the  unordered_map are not sorted in any particular order with respect to either their  key or mapped values, but organized into  buckets depending on their hash values to allow for fast access to individual elements directly by their  key values (with a constant average time complexity on average).

unordered_map containers are faster than  map containers to access individual elements by their  key, although they are generally less efficient for range iteration through a subset of their elements.

Unordered maps implement the direct access operator ( operator[]) which allows for direct access of the  mapped valueusing its  key value as argument.

Iterators in the container are at least  forward iterators.


在C语言中,计算数组中最长连续数字序列(即连续的整数序列)通常涉及动态规划的思想。你可以通过遍历数组,比较当前元素和前一个元素的关系来找到最长的连续序列。以下是基本步骤: 1. 初始化两个变量:`current_length`记录当前连续序列的长度,`max_length`记录全局的最大长度。 2. 遍历数组,假设数组名为`arr`,从第一个元素开始: - 如果当前元素等于前一个元素加一,说明这是一个连续序列,`current_length`加一。 - 否则,将`current_length`与`max_length`比较,如果`current_length`大于`max_length`,更新`max_length`为`current_length`,然后`current_length`重置为1,因为当前序列结束。 3. 遍历结束后,检查最后一次遍历时的`current_length`是否超过`max_length`,如果是,则也需要更新`max_length`。 下面是一个简单的示例函数实现这个功能: ```c #include <stdio.h> int longestConsecutive(int arr[], int n) { if (n == 0) return 0; int current_length = 1, max_length = 1; for (int i = 1; i < n; i++) { if (arr[i] == arr[i-1] + 1 || arr[i] == arr[i-1] - 1) { current_length++; } else { if (current_length > max_length) { max_length = current_length; } current_length = 1; } } // 检查最后一个元素的连续情况 if (current_length > max_length && current_length != 1) { max_length = current_length; } return max_length; } int main() { int arr[] = {100, 4, 200, 1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); printf("Length of the longest consecutive sequence is: %d\n", longestConsecutive(arr, n)); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值