Longest Consecutive Sequence——Leetcode
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.
题目大意:给一个无序数组,找出最大的连续元素的长度。
说真的,一开始这题,我想半天只想到用boolean数组mark,后来看了别人的才发现可以用set做。
解题思路:
把所有的数组放到set中,然后遍历数组的元素,然后分别向前、向后寻找set中是否存在,记录max。
public int longestConsecutive(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
Set<Integer> set = new HashSet<>();
for(int num:nums){
set.add(num);
}
int max = 0;
for(int i=0;i<nums.length;i++){
Integer num = nums[i];
int cnt = 1;
while(set.contains(--num)){
cnt++;
set.remove(num);
}
num=nums[i];
while(set.contains(++num)){
cnt++;
set.remove(num);
}
max=Math.max(max,cnt);
}
return max;
}
Longest Consecutive Sequence——Leetcode的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence -- LeetCode
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Longest Consecutive Sequence [LeetCode]
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest Consecutive Sequence leetcode java
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- [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 ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- A的ascll吗是多少?
//输入一个字符,返回他的ascll码 #include<stdio.h> int main() { char a; while(scanf("%c",&a)! ...
- C#调用cmd程序,读取结果
示例,调用cmd执行PING命令,读取结果,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...
- vsftpd安装指南
如果您用的是Fedora 或Redhat 系统,可以用下面的命令在线安装: [root@localhost ~]# yum install vsftpd 如果是debian 类系统,可以用apt 来在 ...
- 在CentOS6.0上安装Oracle 11gR2 (11.2.0.1)以及基本的配置(一)
首先安装CentOS6.0 就不用说了.安装即可.唯一需要注意的就是后面Oracle 11G Installation guide中的Checking the Software Requireme ...
- python基础(目录)
1.数据库操作入门 2.网络编程入门 3.编码规范 4.测试
- iOS 使用GBK编码的hmacMD5算法
该方法是写在工具类中的,而不是写在NSString的类别中 方法的声明: /** * 使用hmac-md5加密 * * @param clearText 原文 * @param secret ...
- mysql 链接数据库
一.MySQL 连接本地数据库,用户名为“root”,密码“root”(注意:“-p”和“root” 之间不能有空格) C:\>mysql -h localhost -u root -proot ...
- Swift 中的getting和setter的使用
以下简单的介绍Swift中的getting和setting的使用方法: Xcode version: 6.1 先附代码: class Test { var num1: Double = 0.0 ini ...
- 监视/etc/passwd文件是否正常
帮助监视/etc/passwd文件是否正常(P90 练习6.7) 1)找出有UID0的所有项 2)找出有重复UID的所有项 3)找出有重复登录名的所有项 4)找出没有口令的所有项 5)找出没有作废日期 ...
- Apache添加虚拟主机目录
<VirtualHost 127.0.0.2:80> DocumentRoot d:/abcd ServerName 127.0.0.2:80</VirtualHost> &l ...