《leetcode》longest-consecutive-sequence
题目: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.
解析:题目的意思是求最长的连续子序列长度,当然要求时间复杂度为O(n)。
import java.util.Arrays;
public class Solution {public int longestConsecutive(int[] num) {if(num.length==1){return 1;}int max=0;//最大的长度int count=1;//给当前连续的元素计数Arrays.sort(num);//先来个排序for(int i=1;i<num.length;i++){if((num[i]==num[i-1]+1)){//当前的元素是前一个元素+1count++;if(count>=max){max=count;}}else if ((num[i]==num[i-1])){//当前的元素和前一个元素一样,不计数if(count>=max){max=count;}continue;}else{//当前的元素不等于前一个元素,重新计数count=1;if(count>=max){max=count;}}}return max;}
}
《leetcode》longest-consecutive-sequence相关推荐
- leetcode 128. Longest Consecutive Sequence | 128. 最长连续序列(Java)
题目 https://leetcode.com/problems/longest-consecutive-sequence/ 题解 方法1:HashMap 解法,O(n^2) 如下图,假设 n=4 被 ...
- 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》数据结构入门板块
文章目录 <LeetCode题>数据结构入门板块 第1天 数组 217.存在重复元素[简单,哈希表] 53.最大子序和[简单,动态规划,贪心] 第2天 数组 1.两数之和[简单,哈希表] ...
- leetcode Longest Consecutive Sequence
#include <iostream> #include <vector> #include <unordered_map>// 时间复杂度是O(n), 空间复杂度 ...
- LeetCode: Longest Consecutive Sequence
想到map了,可惜没想到用erase来节省空间,看了网上答案 1 class Solution { 2 public: 3 int longestConsecutive(vector<int&g ...
- LeetCode 128. 最长连续序列(Longest Consecutive Sequence)
题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...
最新文章
- Centos6 安装NTP 4.2.8 服务器
- 【HTML】处理<br>换行符追加到前端换行无效的问题 --- html中渲染的字符串中包含HTML标签无效的处理方法,字符串中包含HTML标签被转义的问题 解决
- Asp.Net统一前后端提示信息方案
- python画五角星-Python第八课 绘制五角星1.0
- [转] ios学习--openURL的使用方法
- java运行效率优化_如何优化JAVA代码及提高执行效率
- 微型计算机中AGP指,2011江苏省计算机等级考试二级理论考试试题及答案
- 中国创客面临无限挑战
- 用python 写网络爬虫--零基础
- Spring在多线程中bean的注入问题
- matplotlib输出图形到网页_pytorch使用matplotlib和tensorboard实现模型和训练的可视化...
- .NET URL 301转向方法的实现
- 安装NVIDIA显卡驱动
- 如何将长截图转换成TXT呢?
- java生成zipf分布_用于文本生成的Java中的Zipf定律 – 太慢了
- 什么是CDN加速服务器?
- C语言任意输入三个数构成三角形,随便输入三个数,看能否组成一个三角形
- 智慧城市建设带给安防企业的机遇与挑战
- github下载加速的几种方法
- 股市学习稳扎稳打(四)当宏观经济出现复苏时,不同的行业分别以什么顺序进行轮动上涨
热门文章
- 2PC协议(2-phase-commit protocol)
- C++题解-Leecode 520. 检测大写字母——Leecode每日一题系列
- [leetcode]94.二叉树的中序遍历
- android 属性动画伸缩,Android的属性动画(二)加载框圆点旋转收缩放大缩小效果的实现...
- c语言程序设计编程解读,C语言程序设计第三次实验报告解读
- softirq/tasklet/workqueue的区别
- crm系统是什么很棒ec实力_搭建CRM系统要明确几个步骤?什么样的CRM是真正有用的系统?...
- jet mysql连接字符串,关于jet db的连接字串,以及加密后的字串-数据库专栏,SQL Server...
- java 内省 反射_Java的反射和内省
- 学生签到系统c代码_C语言实现简单学生学籍管理系统