本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43854597

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.

思路:

(1)题意为给定一个未排序的数组,求数组中最长连续元素的个数。

(2)由于数组是未排序的,而需要求得数组中连续序列元素的个数。首先想到的应该是对数组进行排序,本文用的是java类库中自带的排序算法:Arrays.sort()。然后遍历数组,由于数组中可能出现多个连续序列,所以设置当前最长序列个数max和正在遍历的连续序列的个数count。首先,从数组下标为0开始往后遍历,判断当前元素和后续元素是否相差1,如果相差1,则count++,当遍历到倒数第二个元素时,返回count和max的较大值;如果相等,判断max和count大小,将较大值赋给max,继续遍历;其余情况为值相差大于1,这时将max和count较大值赋给max,并将count置为1(因为连续序列在这里断开了,需要重新记录);遍历完整个数组,所得max即为最长连续序列个数。

(3)本文算法效率不是很高,有待后续优化。希望本文对你有所帮助。

算法代码实现如下:

/*** @author liqq*/
public class Longest_Consecutive_Sequence {public int longestConsecutive(int[] num) {if (num == null)  return -1;if (num.length == 1)  return 1;Arrays.sort(num);int count = 1;int max = 1;for (int i = 0; i < num.length - 1; i++) {if (num[i] + 1 == num[i + 1]) {count = count + 1;if (i == num.length - 2) {return count > max ? count : max;}} else if (num[i] == num[i + 1]) {max = count > max ? count : max;continue;} else {max = max > count ? max : count;count = 1;}}return max;}
}

Leetcode_128_Longest Consecutive Sequence相关推荐

  1. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

  2. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  3. LeetCode:Longest Consecutive Sequence

    题目链接 Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  4. [LeetCode] Longest Consecutive Sequence 求解

    为什么80%的码农都做不了架构师?>>>    题目 Given an unsorted array of integers, find the length of the long ...

  5. LeetCode: Longest Consecutive Sequence

    想到map了,可惜没想到用erase来节省空间,看了网上答案 1 class Solution { 2 public: 3 int longestConsecutive(vector<int&g ...

  6. 128. Longest Consecutive Sequence

    Title 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [ ...

  7. leetcode 128. Longest Consecutive Sequence | 128. 最长连续序列(Java)

    题目 https://leetcode.com/problems/longest-consecutive-sequence/ 题解 方法1:HashMap 解法,O(n^2) 如下图,假设 n=4 被 ...

  8. LeetCode 128. 最长连续序列(Longest Consecutive Sequence)

    题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...

  9. leetcode Longest Consecutive Sequence

    #include <iostream> #include <vector> #include <unordered_map>// 时间复杂度是O(n), 空间复杂度 ...

最新文章

  1. 【C++】多线程与并发【一】
  2. Redis和Memcached的一些区别
  3. html5导航菜单置顶,jQuery和css3顶部固定导航菜单特效插件
  4. linux基础命令 sed
  5. 那些年,登山徒步记录,立贴
  6. 当我们群嘲假博士时,不要忘了真博士们的艰辛
  7. 使用.NET为Window Mobile写自动化工具的无奈之处.
  8. 为节省内存,动态添加view布局和控件
  9. 安卓手机投屏到台式电脑非常简单,系统工具就成
  10. maps-api-v3_Google Maps API的自适应设计,视网膜图像和调试
  11. 【微信小程序】断点调试一
  12. eNSP-配置单臂路由与静态路由实验
  13. Afnetworking访问遇到3840_错误
  14. 光脚的快感!仅仅耐克1/8重,一个夏天不会臭脚!徒步不累脚!
  15. Class Test can not access a member of class User with modifiers “private“
  16. 哈工大软件构造笔记1
  17. Sleep()简析 和Sleep(0)的妙用
  18. 列主元高斯消元法(Python实现)
  19. MoCo v1 论文阅读笔记
  20. 日撸力扣三道题---Day3---数组算法+二分查找

热门文章

  1. 吴恩达深度学习笔记-布置机器学习项目(第4课)
  2. 导出excel poi
  3. CreateCompatibleDC,CreateCompatibleBitmap,SelectObject详解
  4. Python3.5 使用 protobuf3.0.0.beta2
  5. Mysql同步数据到Elasticsearch(实时Canal)
  6. 进击的JavaScript(对象,继承,单例模式)
  7. ORACLE+10G+win7下载地址
  8. 利用Python进行数据分析笔记-pandas建模(Patsy篇)
  9. ubuntu 设置动态壁纸来美化桌面(Live Wallpaper)
  10. transition(过渡)