zoukankan      html  css  js  c++  java
  • LeetCode --- Longest Consecutive Sequence

    题目链接

    对unordered_set和set的内部实现还不了解,只知道前者是hash实现的,后者是用R-B-Tree实现。有时间需要读下源码。

    附上代码:

     1 class Solution {
     2 public:
     3     int longestConsecutive(vector<int> &num) {
     4         unordered_set<int> mynum(num.begin(), num.end());
     5         // "max_len" holds the length of the longest consecutive sequence
     6         int max_len(0);
     7         // "N" is the length of "num"
     8         unsigned int N(num.size());
     9         // iterator every element of vector num
    10         for (unsigned int i(0); i < N; i++) {
    11             // "count" holds the maximum length of 
    12             //  consecutive sequence that can be found 
    13             //  by expanding from "num[i]" to the left and right
    14             int count(0), tmp(num[i]);
    15             while (mynum.find(tmp) != mynum.end()) {
    16                 // erase "tmp" from set
    17                 mynum.erase(tmp);
    18                 count++;
    19                 // since "INT_MAX + 1 = INT_MIN"
    20                 // we should not consider {INT_MAX-1, INT_MAX, INT_MIN} as consecutive sequence 
    21                 if (tmp == INT_MAX) break;
    22                 tmp++;
    23             }
    24             tmp = nu
  • 相关阅读:
    git上传本地代码
    oracle 时间格式转化以及计算
    windows 下使用sqlplus
    filter 过滤器加载流程
    vscode java
    vscode cpp
    manual jar
    toy jar
    inteli shortcut
    eng
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3758832.html
Copyright © 2011-2022 走看看