高级农民
- 积分
- 2244
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-2-24
- 最后登录
- 1970-1-1
|
- public class Solution {
- public int longestConsecutive(int[] nums) {
- if(nums.length==0)
- return 0;
- HashSet<Integer> set = new HashSet<Integer>();
- for(int e : nums)
- set.add(e);
-
- int max=1;
-
- for(int e:nums)
- {
- int left = e-1;
- int right = e+1;
- int count = 1;
-
- while(set.contains(left))
- {
- count++;
- set.remove(left);
- left--;
- }
-
-
- while(set.contains(right))
- {
- count++;
- set.remove(right);
- right++;
- }
-
- max=Math.max(max,count);
- }
- return max;
- }
- }
复制代码 |
|