为什么80%的码农都做不了架构师?>>>   

题目

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(1)时间查找该数字是否存在于数组当中

  • 数字有可能重复,所以其实哈希集合就够了,典型的空间换时间

  • (预处理)一个表征是否使用的used表,用于o(1)时间查找该数字是否已经被包含在另外一个序列当中

  • 注意数字有可能是负数,所以直接用数组不好

  • 轮询数组,遇到一个数字,查找其左,右的最长连续序列长度,并记录与已知最大长度相比较

代码

public class Solution {public int longestConsecutive(int[] num) {Map<Integer, Integer> map = new HashMap<Integer, Integer>();for (int i = 0; i < num.length; i++) {map.put(num[i], i);}Map<Integer, Boolean> used = new HashMap<Integer, Boolean>();for (int i : num) {used.put(i, false);}int max = Integer.MIN_VALUE;for (int i = 0; i < num.length; i++) {if (!used.get(num[i])) {used.put(num[i], true);int k = num[i];int leftLength = findLength(k, map, "left");int rightLength = findLength(k, map, "right");// mark usedfor (int j = 0; j < leftLength; j++) {used.put(k - j - 1, true);}for (int j = 0; j < rightLength; j++) {used.put(k + j + 1, true);}int total = leftLength + rightLength + 1;if (total > max) {max = total;}}}return max;}private int findLength(int k, Map<Integer, Integer> map, String direction) {if ("left".equals(direction)) {int l = k - 1;while (map.containsKey(l)) {l -= 1;}return k - l - 1;} else {int l = k + 1;while (map.containsKey(l)) {l += 1;}return l - k - 1;}}
}

转载于:https://my.oschina.net/zuoyc/blog/338719

[LeetCode] Longest Consecutive Sequence 求解相关推荐

  1. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  2. LeetCode:Longest Consecutive Sequence

    题目链接 Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  3. leetcode Longest Consecutive Sequence

    #include <iostream> #include <vector> #include <unordered_map>// 时间复杂度是O(n), 空间复杂度 ...

  4. LeetCode: Longest Consecutive Sequence

    想到map了,可惜没想到用erase来节省空间,看了网上答案 1 class Solution { 2 public: 3 int longestConsecutive(vector<int&g ...

  5. [leetcode]Longest Consecutive Sequence

    这道题目一看就去翻答案了,因为以前见过,知道并查集可以用来做分组,但这样的题目真的要并查集么?想想也是HashSet之类就能搞定么.果然是HashMap而不是HashSet.下面这个解法我比较容易理解 ...

  6. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

  7. leetcode 128. Longest Consecutive Sequence | 128. 最长连续序列(Java)

    题目 https://leetcode.com/problems/longest-consecutive-sequence/ 题解 方法1:HashMap 解法,O(n^2) 如下图,假设 n=4 被 ...

  8. LeetCode 128. 最长连续序列(Longest Consecutive Sequence)

    题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...

  9. 128. Longest Consecutive Sequence

    Title 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [ ...

最新文章

  1. table 隔列换色
  2. python里面两个大于号_【课堂笔记】Python常用的数值类型有哪些?
  3. 计算机运维知识题库,(哭求哥哥姐姐帮助一下)计算机维护笔试题库(辛苦了)...
  4. 用python玩转数据测试与作业_用Python玩转数据分析10
  5. 插件不既有Chrome版也有飞鸽传书
  6. Oracle中较长number型数值的科学计数显示问题
  7. JDBC是什么?和JDBC程序的基本编写
  8. gsp计算机系统测试题答案,新版GSP计算机系统专业知识培训测试题
  9. python做t检验_Python实现--假设检验:T检验
  10. 美食杰 login的实现效果
  11. 在线诱舞坊字体转换FLASH工具 QQ空间专用字体转换器
  12. cocoscreator的游戏背景适配方案
  13. win10右键 发送到 选项消失解决办法
  14. 在线问答系统---数据库管理
  15. 编程实战——电影管理器之界面UI及动画切换
  16. ocp跟oce的区别 oracle_六种角度看OCA与OCP、OCM区别
  17. 后悔当初学php,中佛网-法师开示:当初为了家庭而丢了事业,现后悔不已,该怎么办?...
  18. choose (when, otherwise)
  19. 录音转文字软件有电脑版吗?推荐这几个音频转文字软件
  20. Fabric中PBFT源码解读——Checkpoint机制

热门文章

  1. 在日常生活中,经常会遇到某些需求对文件名称进行修改,借助刚学过的文件操作,编写一个可以批量修改文件名的小程序。
  2. 如何用js给图片重置宽_如何用js给老婆每天发情话
  3. mysql 字符转换函数是_MySQL日期和字符串转换函数
  4. android 查询所有图片和视频,Android系统详解之获取图片和视频的缩略图
  5. 2011年计算机一级考试题,2011年计算机一级考试试题及答案
  6. java 返回两个集合_使用 java stream 获取两个集合之间的交集.差集
  7. 插入排序之表插入排序
  8. core文件如何分析
  9. 动态设置html字号,动态设置html的font-size值 (适配文字大小)
  10. oracle sql优化指导,基于Oracle的sql优化(1)