LeetCode:110 平衡二叉树

class Solution {
    public int longestConsecutive(int[] nums) {
        if(nums==null){
            return 0;
        }
        Arrays.sort(nums);
        PriorityQueue<Integer> que = new PriorityQueue<Integer>((a,b)->a.compareTo(b));
        int len = nums.length;
        int t=0;
        int tmp=0;
        for(int i=0;i<len;i++){
            if(que.size()==0){
                tmp = nums[0];
                que.add(nums[0]);
                t++;
                continue;
            }

            if(nums[i]==tmp+1){
                tmp =  nums[i];
                que.add(nums[i]);
                t=Math.max(que.size(),t);
            }
            else if(nums[i]==tmp){
                continue;
            }
            else{
                que.clear();
                tmp = nums[i];
                que.add(nums[i]);

            }

        }
        return t;
    }
}

 

posted @ 2020-10-07 09:06  dlooooo  阅读(84)  评论(0编辑  收藏  举报