leetcode 128. Longest Consecutive Sequence
来源:互联网 发布:慕课平台有哪些 知乎 编辑:程序博客网 时间:2024/06/14 04:15
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.
这道题我本来想,直接用 bucket 解决就好了啊。直到我看到了如下的测试用例。。。
int[] nums=new int[]{0,-1};int[] nums=new int[]{2147483646,-2147483647,0,2,2147483644,-2147483645,2147483645};好吧,那只能用其他方法了。
public int longestConsecutive(int[] nums) {HashSet<Integer> hashSet=new HashSet<Integer>();for(int i=0;i<nums.length;i++){hashSet.add(nums[i]);}int maxConsecutive=0;for(int i=0;i<nums.length;i++){int num=nums[i];int currentConsecutive=1;int val=num;while(hashSet.contains(val-1)){currentConsecutive++;hashSet.remove(val-1);val=val-1;}val=num;while(hashSet.contains(val+1)){currentConsecutive++;hashSet.remove(val+1);val=val+1;}if(currentConsecutive>maxConsecutive){maxConsecutive=currentConsecutive;}}return maxConsecutive;}另外有个大神也用了 hashmap,但是思路不太一样。
key 存储:连续序列的边界点
value 存储:该连续序列的连续个数。
如:对于连续序列 {1, 2, 3, 4, 5}, map.get(1) and map.get(5) 都应该返回 5。
当需要 num 数组中的某数 n 时,做以下的事情:
查看 n - 1 和 n + 1 是否存在在 map 中,如果存在,这意味着有一个已存在的连续序列在 n 的旁边 。其中,变量 left 和 right 存储了左右连续序列的长度。当 left 或 right 为 0 时代表了那一边没有连续序列, n 将会是边界点,那么可以向 map 中存储 n 作为key, (left + right + 1) 作为 value。
public int longestConsecutive(int[] num) { int res = 0; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int n : num) { if (!map.containsKey(n)) { int left = (map.containsKey(n - 1)) ? map.get(n - 1) : 0; int right = (map.containsKey(n + 1)) ? map.get(n + 1) : 0; // sum: length of the sequence n is in int sum = left + right + 1; map.put(n, sum); // keep track of the max length res = Math.max(res, sum); // extend the length to the boundary(s) // of the sequence // will do nothing if n has no neighbors map.put(n - left, sum); map.put(n + right, sum); } else { // duplicates continue; } } return res;}
阅读全文
0 0
- [LeetCode]128.Longest Consecutive Sequence
- [Leetcode] 128. Longest Consecutive Sequence
- [leetcode] 128.Longest Consecutive Sequence
- [leetcode] 128.Longest Consecutive Sequence
- Leetcode 128. Longest Consecutive Sequence
- Leetcode 128. Longest Consecutive Sequence
- LeetCode 128. Longest Consecutive Sequence
- LeetCode 128. Longest Consecutive Sequence
- [LeetCode]128. Longest Consecutive Sequence
- 【leetcode】128. Longest Consecutive Sequence
- Leetcode 128. Longest Consecutive Sequence
- leetcode.128. Longest Consecutive Sequence
- [leetcode] 128. Longest Consecutive Sequence
- LeetCode-128.Longest Consecutive Sequence
- Leetcode 128. Longest Consecutive Sequence
- [LeetCode] 128. Longest Consecutive Sequence
- LeetCode 128. Longest Consecutive Sequence
- [LeetCode]128. Longest Consecutive Sequence
- bzoj 2306: [Ctsc2011]幸福路径
- bzoj 2044三维导弹拦截 二分图匹配
- xlistView_header的布局
- ORB_SLAM编译环境搭建过程当中遇到的问题
- 131. Palindrome Partitioning
- leetcode 128. Longest Consecutive Sequence
- Head First设计模式之迭代器模式
- No valid bower.json was found in any branch ...
- Python字符串格式化--format()方法
- 235. Lowest Common Ancestor of a Binary Search Tree
- 数据结构C语言链栈
- 51Nod 1384 全排列(next-permutation)
- P1308 统计单词数
- java中String数组和List的互相转化