Online SR (TSC center) ?# EB23 - 劳工卡
c*p
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;
}
}
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
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
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;
}
}