上年被google问到。。结果呆住了,,其实还好啦
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | public class Solution {
public int longestConsecutive(int[] nums) {
HashSet<Integer> lookup = new HashSet<Integer>();
for (int i = 0; i < nums.length; i++){
lookup.add(nums[i]);
}
int max = 0;
for (int i = 0; i < nums.length && lookup.size() != 0; i++){
if (lookup.contains(nums[i])){
int localMax = 1;
int startingPoint = nums[i];
int temp = startingPoint+1;
while (lookup.contains(temp)){
lookup.remove(temp++);
localMax++;
}
temp = startingPoint-1;
while (lookup.contains(temp)){
lookup.remove(temp--);
localMax++;
}
max = Math.max(localMax, max);
}
}
return max;
}
}
|
No comments:
Post a Comment