认认真真写了了并查集没过,超时了哈哈。
看了下大家的思路,嗯果然 简简单单用个 Set 就行

import java.util.*;


public class Solution {
    /**
     * max increasing subsequence
     * @param arr int整型一维数组 the array
     * @return int整型
     */

    public int MLS (int[] arr) {
        // write code here
            if(arr == null || arr.length == 0){return 0;}
            Set<Integer> set = new HashSet<>();
            for(int ele: arr){ set.add(ele); }
            int maxLen = 0;
            for(int i = 0; i < arr.length; ++i){
                int ele = arr[i], len = 1;
                if(!set.contains(ele + 1)){
                    while(set.contains(--ele)){ ++len; }
                    maxLen = Math.max(maxLen, len);
                }
            }
            return maxLen;
    }


    public int MLS2 (int[] arr) {
        // write code here

        ArrayList<Integer> arrayList=new ArrayList<>(16);
        Set<Integer> set=new HashSet<>(16);
        for (int i:arr){
            if (set.contains(i)){
                continue;
            }
            set.add(i);
            arrayList.add(i);
        }

        UnionSet<Integer> unionSet=new UnionSet<Integer>(arrayList);



        for (int i=0;i<arrayList.size();i++){
            for (int j=i;j<arrayList.size();j++){
                if (Math.abs(arrayList.get(i)-arrayList.get(j))==1){
                    unionSet.union(arrayList.get(i),arrayList.get(j));
                }
            }
        }
        return unionSet.maxSize;
    }

    //首先实现并查集

    public static class MyNode<V>{
        V value;

        public MyNode(V v){
            value=v;
        }
    }

    public static class UnionSet<V>{

        public HashMap<V,MyNode<V>> nodes;
        public HashMap<MyNode<V>,MyNode<V>> parents;
        public HashMap<MyNode<V>,Integer> sizeMap;
        public Integer maxSize=0;

        public UnionSet(ArrayList<V> list){

            if(list==null){
                return;
            }
            nodes=new HashMap();
            parents=new HashMap();
            sizeMap=new HashMap();
            maxSize=1;

            for(V item:list){
                MyNode<V> node=new MyNode<>(item);
                nodes.put(item,node);
                parents.put(node,node);
                sizeMap.put(node,1);
            }

        }


        private MyNode<V> findFather(MyNode<V> cur){

            MyNode<V> father=cur;
            Stack<MyNode<V>> stack=new Stack<>();
            while(father!=parents.get(father)){
                stack.add(father);
                father=parents.get(father);
            }
            while(!stack.isEmpty()){
                parents.put(stack.pop(),father);
            }
            return father;
        }

        public void union(V a,V b){
            if(!nodes.containsKey(a)||!nodes.containsKey(b)){
                return;
            }

            MyNode<V> aFather=findFather(nodes.get(a));
            MyNode<V> bFather=findFather(nodes.get(b));

            if(aFather!=bFather){

                Integer aSize=sizeMap.get(aFather);
                Integer bSize=sizeMap.get(bFather);
                Integer unionSize=aSize+bSize;
                MyNode<V> small=aSize>bSize?bFather:aFather;
                MyNode<V> big=aSize>bSize?aFather:bFather;

                parents.put(small,big);
                sizeMap.put(big,unionSize);
                sizeMap.remove(small);

                if(maxSize<unionSize){
                    maxSize=unionSize;
                }
            }

        }


    }
}