由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Longest Consecutive Sequence 问题释疑
相关主题
leetcode longest consecutive sequence怎么做Post 1 question: Bits operation using C programming
leetcode longest consecutive sequence还是想不通!请问longest common consecutive sequence用什么算法?
Random Array number, Find longest consecutive sequence面试的时候可以用TreeSet或者TreeMap之类的数据结构嘛
二爷的那个Longest Consecutive Sequence的新解法?F家伪面经,求bless
请问leetcode 上那道Longest Consecutive Sequence题分享几个FGTP Internship 的面经,顺便求FG收留
问个近来看到的狗家题:longest consecutive sequence in tree请问binary tree longest consecutive number这题的思路
面试题热乎乎的Z家面经
来个面试题目 比较简单lc的题目质量越来越差了吧
相关话题的讨论汇总
话题: fd话题: int话题: mx话题: num
进入JobHunting版参与讨论
1 (共1页)
g***j
发帖数: 1275
1
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
Your algorithm should run in O(n) complexity.
我在网上看到了几段code,用到了set或者map,每次要在set或者map里面找到,然后删
掉,我想问,这样的code时间是n么?find in a set难道不是logn么?当然也有用到
map的,我觉得找元素都是logn啊,那么最终的时间就是nlogn啊,比如如下code
class Solution {
public:
int longestConsecutive(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
mapmp;
for (int i=0;i mp[num[i]]=true;
}

int res=0;
for (int i=0;i int mx=1;
int fd = num[i];

mp.erase(num[i]);
while (mp.find(fd+1)!=mp.end()){
mx++;
mp.erase(fd+1);
fd++;
}

fd = num[i];
while (mp.find(fd-1)!=mp.end()){
mx++;
mp.erase(fd-1);
fd--;
}

if (mx>res){res=mx;}
}

return res;
}
};
r*******e
发帖数: 7583
2
set/map不一定是ordered,看实际需要
像这个例子里的,用hash_set/hash_map实现就是O(1)查找了

length

【在 g***j 的大作中提到】
: 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
: Your algorithm should run in O(n) complexity.
: 我在网上看到了几段code,用到了set或者map,每次要在set或者map里面找到,然后删
: 掉,我想问,这样的code时间是n么?find in a set难道不是logn么?当然也有用到
: map的,我觉得找元素都是logn啊,那么最终的时间就是nlogn啊,比如如下code
: class Solution {

s**********r
发帖数: 8153
3
这个题我也没明白:(
1 (共1页)
进入JobHunting版参与讨论
相关主题
lc的题目质量越来越差了吧请问leetcode 上那道Longest Consecutive Sequence题
几道面试题问个近来看到的狗家题:longest consecutive sequence in tree
zenefits店面(已挂)面试题
G题讨论来个面试题目 比较简单
leetcode longest consecutive sequence怎么做Post 1 question: Bits operation using C programming
leetcode longest consecutive sequence还是想不通!请问longest common consecutive sequence用什么算法?
Random Array number, Find longest consecutive sequence面试的时候可以用TreeSet或者TreeMap之类的数据结构嘛
二爷的那个Longest Consecutive Sequence的新解法?F家伪面经,求bless
相关话题的讨论汇总
话题: fd话题: int话题: mx话题: num