Leetcode_128_Longest Consecutive Sequence
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- Gradle 1.12用户指南翻译——第四十七章. Build Init 插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- 十大豪门推送sdk,哪个更适合你
转自:http://jingyan.baidu.com/article/d621e8da0fd7042865913ff5.html 推送,使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户 ...
- 一道有趣的Twitter技术面试题
来自:http://blog.jobbole.com/50705/ 看下面这个图片” “在这个图片里我们有不同高度的墙.这个图片由一个整数数组所代表,数组中每个数是墙的高度.上边的图可以表示为数组[2 ...
- Android中Snackbar的介绍以及使用
Android中Snackbar的介绍以及使用 介绍 Snackbar可以说是Toast的升级版,不仅有显示信息的功能,还可以添加一个Action,实现点击功能,可以右滑删除. 效果图 Snackba ...
- mysql 远程连接配置
近期买了阿里云服务器,服务器 安装了mysql,需要远程操作mysql数据库,但是远程不配置的话,连接不上去的.需要配置 .具体的配置如下: 先看看my.cnf是否绑定了本机,如果绑定了地址就解绑吧. ...
- collection 中对类排序
首先 写出 一个person类 让他继承Comparable 构造函数和get/set不用说 我们要覆盖父类中的comparto方法 代码如下 省略get/set package a; public ...
- GDAL不支持创建PCIDSK的面状矢量格式
最近在使用GDAL创建PCIDSK格式的矢量数据,发现创建点和线的矢量数据都没问题,创建面状的只有属性表没有图形.在GDAL官网说明也写的是支持的,地址为:http://www.gdal.org/fr ...
- UNIX网络编程——客户/服务器程序设计示范(三)
TCP预先派生子进程服务器程序,accept无上锁保护 我们的第一个"增强"型服务器程序使用称为预先派生子进程的技术.使用该技术的服务器不像传统意义的并发服务器那样为每个客户现场派 ...
- Dynamics CRM2015 Custom Code Validation Tool工具的使用
工具下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=45535 下载后双击exe文件解压后里面会有个zip文件,将文件导入系 ...
- springMVC 使用ajax 出现No serializer found for class异常
转自 http://mxdba.iteye.com/blog/668155 google了一下,发现坛子里已经有人解答了 http://godfox.iteye.com/blog/646887 不过 ...