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相关推荐
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- LeetCode:Longest Consecutive Sequence
题目链接 Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- [LeetCode] Longest Consecutive Sequence 求解
为什么80%的码农都做不了架构师?>>> 题目 Given an unsorted array of integers, find the length of the long ...
- LeetCode: Longest Consecutive Sequence
想到map了,可惜没想到用erase来节省空间,看了网上答案 1 class Solution { 2 public: 3 int longestConsecutive(vector<int&g ...
- 128. Longest Consecutive Sequence
Title 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [ ...
- leetcode 128. Longest Consecutive Sequence | 128. 最长连续序列(Java)
题目 https://leetcode.com/problems/longest-consecutive-sequence/ 题解 方法1:HashMap 解法,O(n^2) 如下图,假设 n=4 被 ...
- LeetCode 128. 最长连续序列(Longest Consecutive Sequence)
题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...
- leetcode Longest Consecutive Sequence
#include <iostream> #include <vector> #include <unordered_map>// 时间复杂度是O(n), 空间复杂度 ...
最新文章
- 【C++】多线程与并发【一】
- Redis和Memcached的一些区别
- html5导航菜单置顶,jQuery和css3顶部固定导航菜单特效插件
- linux基础命令 sed
- 那些年,登山徒步记录,立贴
- 当我们群嘲假博士时,不要忘了真博士们的艰辛
- 使用.NET为Window Mobile写自动化工具的无奈之处.
- 为节省内存,动态添加view布局和控件
- 安卓手机投屏到台式电脑非常简单,系统工具就成
- maps-api-v3_Google Maps API的自适应设计,视网膜图像和调试
- 【微信小程序】断点调试一
- eNSP-配置单臂路由与静态路由实验
- Afnetworking访问遇到3840_错误
- 光脚的快感!仅仅耐克1/8重,一个夏天不会臭脚!徒步不累脚!
- Class Test can not access a member of class User with modifiers “private“
- 哈工大软件构造笔记1
- Sleep()简析 和Sleep(0)的妙用
- 列主元高斯消元法(Python实现)
- MoCo v1 论文阅读笔记
- 日撸力扣三道题---Day3---数组算法+二分查找
热门文章
- 吴恩达深度学习笔记-布置机器学习项目(第4课)
- 导出excel poi
- CreateCompatibleDC,CreateCompatibleBitmap,SelectObject详解
- Python3.5 使用 protobuf3.0.0.beta2
- Mysql同步数据到Elasticsearch(实时Canal)
- 进击的JavaScript(对象,继承,单例模式)
- ORACLE+10G+win7下载地址
- 利用Python进行数据分析笔记-pandas建模(Patsy篇)
- ubuntu 设置动态壁纸来美化桌面(Live Wallpaper)
- transition(过渡)