Longest Consecutive Sequence
来源:互联网 发布:javascript dom和框架 编辑:程序博客网 时间:2024/06/05 18:50
问题解法1:
We will use HashMap. The key thing is to keep track of the sequence length and store that in the boundary points of the sequence. For example, as a result, for sequence {1, 2, 3, 4, 5}, map.get(1) and map.get(5) should both return 5.
Whenever a new element n is inserted into the map, do two things:
- See if n - 1 and n + 1 exist in the map, and if so, it means there is an existing sequence next to n. Variables left and right will be the length of those two sequences, while 0means there is no sequence and n will be the boundary point later. Store (left + right + 1) as the associated value to key n into the map.
- Use left and right to locate the other end of the sequences to the left and right of n respectively, and replace the value with the new length.
Everything inside the for loop is O(1) so the total time is O(n). Please comment if you see something wrong. Thanks.
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;}
用python 实现的相同代码:
class Solution(object): def longestConsecutive(self, nums): """ :type nums: List[int] :rtype: int """ dict ={} res = 0 for num in nums: if dict.get(num) == None: left = dict.get(num - 1) right = dict.get(num + 1) if left == None: left = 0 if right == None: right = 0 sum = left + right + 1 dict[num] = sum; dict[num - left] = sum dict[num + right] = sum res = max(res, sum) return res;
阅读全文
0 0
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- Leetcode : Longest Consecutive Sequence
- [Leetcode] Longest Consecutive Sequence
- longest consecutive elements sequence
- [leetcode] longest consecutive sequence
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- 【leetcode】Longest Consecutive Sequence
- LeetCode - Longest Consecutive Sequence
- [LeetCode]Longest Consecutive Sequence
- leetcode:Longest Consecutive Sequence
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- [Leetcode]Longest Consecutive Sequence
- [leetcode]Longest Consecutive Sequence
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- 前台经验---梳理下radio和checkbox的页面获取问题
- js 交换两个元素的位置,动画效果
- 页面内滚动条样式修改
- Java transient关键字
- React Native starter project bundling fails with Unexpected Token error
- Longest Consecutive Sequence
- 快学numpy04
- 优先级翻转与优先级继承
- 欧拉函数
- 倒序删除子物体
- NYOJ214单调递增子序列(二)
- WebRTC之框架与接口(二)
- 如何成为一个顶级的Java工程师(修改自成神之路)
- myeclipse格式化代码设置