您的位置:首页 > 产品设计 > UI/UE

longest-consecutive-sequence

2016-06-29 20:47 381 查看
import java.util.*;

public class Solution {

    public int longestConsecutive(int[] num) {

        Set<Integer> set =new HashSet<Integer>();

        for(int i=0;i<=num.length-1;i++)

        {

            set.add(num[i]);

        }

        int max=1;

        for(int e:num)

            {

            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;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: