由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode longest consecutive sequence还是想不通!
相关主题
leetcode longest consecutive sequence怎么做几道面试题
Ask a google interview question(2)攒人品,twitter二面面经
Longest Consecutive Sequence 问题释疑hashmap跟hash table有啥区别?
请问leetcode 上那道Longest Consecutive Sequence题leetcode 4sum
Random Array number, Find longest consecutive sequenceleetcode 上的 two sum
二爷的那个Longest Consecutive Sequence的新解法?LeetCode 的 4 sum 问题 如何用hash table做呢?
问个近来看到的狗家题:longest consecutive sequence in treeInterview Question I Got
面试题请教leetcode上的count and say
相关话题的讨论汇总
话题: integer话题: int话题: value话题: num话题: hash
进入JobHunting版参与讨论
1 (共1页)
c********p
发帖数: 1969
1
没人理了,只好又发一次。。。
http://www.mitbbs.com/article_t0/Java/31140155.html
这回我按网友建议把求hashtable size改成直接count删去的个数了,还是不能通过大
oj。所以又跑来问问我到底哪里出了问题?!
http://www.mitbbs.com/article_t0/JobHunting/32465791.html
原帖在这里。
我新的代码:
import java.util.*;
public class Solution {
public int longestConsecutive(int[] num) {

if(num == null || num.length == 0){
return 0;
}
int max = 1;
int count = 1;
Hashtable hash = new Hashtable();
for(int i = 0; i < num.length; i++){
if(!hash.containsKey(num[i])){
hash.put(num[i], 1);
}
}

for(int i = 0; i < num.length; i++){
if(hash.containsKey(num[i])){
int value = num[i];
count = 1 + check(hash, value + 1, 1) + check(hash,
value - 1, -1);
hash.remove(num[i]);
max = Math.max(max, count);
}
}
return max;
}
public int check(Hashtable hash, int value, int flag){
if(hash.containsKey(value)){
if(flag == 1){
hash.remove(value);
return 1 + check(hash, value + 1, flag);
}else if(flag == -1){
hash.remove(value);
return 1 + check(hash, value - 1, flag);
}
}
return 0;
}
}
h******3
发帖数: 351
2


【在 c********p 的大作中提到】
: 没人理了,只好又发一次。。。
: http://www.mitbbs.com/article_t0/Java/31140155.html
: 这回我按网友建议把求hashtable size改成直接count删去的个数了,还是不能通过大
: oj。所以又跑来问问我到底哪里出了问题?!
: http://www.mitbbs.com/article_t0/JobHunting/32465791.html
: 原帖在这里。
: 我新的代码:
: import java.util.*;
: public class Solution {
: public int longestConsecutive(int[] num) {

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教leetcode上的count and sayRandom Array number, Find longest consecutive sequence
在线紧急求助一道system design面试题,面经内附二爷的那个Longest Consecutive Sequence的新解法?
a problem from leetcode: high efficiency algorithm for combinations problem问个近来看到的狗家题:longest consecutive sequence in tree
HashTable相关的面试题面试题
leetcode longest consecutive sequence怎么做几道面试题
Ask a google interview question(2)攒人品,twitter二面面经
Longest Consecutive Sequence 问题释疑hashmap跟hash table有啥区别?
请问leetcode 上那道Longest Consecutive Sequence题leetcode 4sum
相关话题的讨论汇总
话题: integer话题: int话题: value话题: num话题: hash