Leetcode049--最长连续子数组
来源:互联网 发布:apache ant windows 编辑:程序博客网 时间:2024/06/05 09:49
一、原题
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.
一、中文
给出一个数组,求出里面能够组成的最长连续子数组的长度并返回
三、举例
比如数组[100, 4, 200, 1, 3, 2]中的最长子数组的长度就是[1, 2, 3, 4]
四、思路
首先对数组进行排序,然后将排序后的数组进行查找,从最左边开始,如果相邻就加一,如果不相邻就将其置零
五、程序
import java.util.*;public class Solution { //发现连续的子字符串 public int longestConsecutive(int[] num) { //排序 Arrays.sort(num); int max = 1; int count = 1; for(int i = 1; i < num.length; i++){ if((num[i] - num[i-1]) == 1){ count++; if(count > max){ max = count; } }else if(num[i] - num[i-1] == 0){ continue; }else{ count = 1; } } return max; }}
阅读全文
1 0
- Leetcode049--最长连续子数组
- 最长连续子数组
- 最长连续子数组和为0
- 最长连续子数组的和
- 求最长连续子数组问题
- 数组的最长-不连续-递增子数组-O(nlogn)
- 找出无序数组的最长连续子数组
- 连续最长子序列
- 最长连续子序列
- 最长连续子序列
- 最长连续子序列
- LCP数组的实现和最长公共连续子串
- 求数组中最长的连续子序列
- poj 2774 后缀数组(最长连续子串)
- 无序数组中找到最长连续子序列
- 求数组中最长连续递增子序列
- 数组最长连续递增(+1)子序列
- fjnu 最长连续子序列
- 单链表入队的一点理解
- tomcat 单独发布项目 和 eclipse启动tomcat的区别
- 图像轮廓匹配
- 菜鸟学习SSH——目录
- android studio连接手机
- Leetcode049--最长连续子数组
- 选择适合自己的Markdown编辑器
- jQuery File Upload-jQuery上传插件
- Spring Framework 介绍
- FreeBSD鱼丸系列之一:VM初始配置
- 软件光栅化渲染器(八)
- Largest prime factor
- xml的约束引用
- Docker阿里云镜像加速