Longest Consecutive Sequence 数组连续数字的情况

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.

寻找最大的连续数组的长度,利用两个set数组,一个数组存储已经被访问过的数组,如果已经被访问过了,那么就不需要再被访问了

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        if(nums.empty())
        {
            return 0;
        }
        unordered_set<int> existSet;
        unordered_set<int> visitedSet;
        int maxLength = 0;
        for(int i = 0; i < nums.size(); i++)
            existSet.insert(nums[i]);
        for(int i = 0; i < nums.size(); i++)
        {
            int length = 0;
            if(visitedSet.count(nums[i]))
            {
                continue;
            }
            else
            {
                visitedSet.insert(nums[i]);
                length++;
                int left = nums[i];
                int right = nums[i];
                while(existSet.count(--left))
                {
                    visitedSet.insert(left);
                    length++;
                }
                while(existSet.count(++right))//<必须使用?前++
                {
                    visitedSet.insert(right);
                    length++;
                }
                maxLength = max(maxLength,length);
            }
        }
        return maxLength;
    }
};

First Missing Positive

Given an unsorted integer array, find the first missing positive integer.

For example,

Given [1,2,0] return 3,

and [3,4,-1,1] return 2.

连续数组求最小的miss的数据,还是利用前面介绍的方法,还是利用两个不同的set,其中一个表示已经访问过了的情况,只观察比当前大的数据,

如果已经访问过了就不访问了

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        if(nums.empty())
        {
            return 1;
        }
        int missVal = INT_MAX;
        int minVal = 1;
        unordered_set<int> numSet;
        unordered_set<int> visitedSet;
        for_each(nums.begin(),nums.end(),[&numSet](int x)
        {
            if(x >= 0)
                numSet.insert(x);
        });
        for(int i = 0; i < nums.size(); i++)
        {
            if(nums[i] < 0 || visitedSet.count(nums[i]))
            {
                continue;
            }
            int right = nums[i];
            while(numSet.count(++right))
            {
                visitedSet.insert(right);
            }
            missVal = min(missVal,right);
        }
        if(numSet.count(1))
            return missVal;
        else
        {
            return 1;
        }
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-06 08:53:54

Longest Consecutive Sequence 数组连续数字的情况的相关文章

[LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

【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

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 OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

Leetcode 贪心 Longest Consecutive Sequence

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions 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 consec

LeetCode: Longest Consecutive Sequence 解题报告

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.

[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,

Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p