Leetcode_128_Longest Consecutive Sequence

时间:2021-12-08 05:00:21

本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. &lbrack;LeetCode&rsqb; Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  2. &lbrack;LeetCode&rsqb; Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  4. 【leetcode】Longest Consecutive Sequence(hard&rpar;&star;

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. 128&period; Longest Consecutive Sequence&lpar;leetcode&rpar;

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. &lbrack;LintCode&rsqb; Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...

  7. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  8. 24&period; Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  9. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

随机推荐

  1. 多线程 thread和Task的用法以及注意事项

    并行 多核线程:Task 首先引用System.Threading; 1:用静态方法:Task.Factory.StartNew()来创建了一个最简单的Task: Task.Factory.Start ...

  2. Android Parcelable和Serializable的区别,androidparcelable

    本文主要介绍Parcelable和Serializable的作用.效率.区别及选择,关于Serializable的介绍见Java 序列化的高级认识. 1.作用 Serializable的作用是为了保存 ...

  3. ST-Link STVP Cannot communicate with the device&excl;

    用STLink在ST Visual Programmer中对STM8下载二进制文件有时会出现: 原因:多半是STM8目标板没有电源有问题,或是电源引脚虚焊:

  4. 【python】命令行神器 Click 简明笔记

    全文拷贝自 命令行神器 Click 简明笔记 Click Click 是用 Python 写的一个第三方模块,用于快速创建命令行.我们知道,Python 内置了一个 Argparse 的标准库用于创建 ...

  5. 2017年7月最新浏览器市场份额,IE8份额仅剩个位数

    数据来源为百度统计所覆盖的超过150万的站点,样本为2017年6月1日-2017年6月30日最新一个月的数据. 统计如下: 其中IE8的份额为9.83%,首次降至个位数.在所有IE版本中,份额最高的是 ...

  6. IntelliJ IDEA执行maven 跳过test

  7. redis水平扩展实践,完全配置,无需代码改动

    设计思路 思路很简单,就是基于用户ID进行分库,将用户的ID字符串按照byte逐个计算ID对应的hash原值(一个数字,取绝对值,因为原始值可能过大溢出,变成负数),然后,再用这个hash原值对库的个 ...

  8. 尚硅谷springboot学习7-yaml配置文件

    SpringBoot使用一个全局的配置文件,配置文件名是固定的: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值 ...

  9. 学习笔记:The Best of MySQL Forum

    http://mysql.rjweb.org/bestof.html I have tagged many of the better forum threads. 'Better' is based ...

  10. 解决ssh&sol;scp报错:Someone could be eavesdropping on you right now &lpar;man-in-the-middle attack&rpar;&excl;

    主要现象:ssh/scp 失败,host key verification failed. # scp /home/iso/********.iso root@192.168.1.***:/home/ ...