参考题解 : https://blog.nowcoder.net/n/7e7dd99b7ec24873a4e20a571ccf6a1a
/* * 思路: * 1.将数组排序 * 2.遍历数组,连续就+1;重复就跳过;不连续就重置 * 3.获取最长数列 **/ public int MLS (int[] arr) { // write code here if(arr == null || arr.length == 0){ return 0; } Arrays.sort(arr); int len = arr.length; int count = 1; int res = 1; for(int i = 1; i < len ; i++){ if(arr[i] == arr[i-1] + 1){ count++; }else if(arr[i] == arr[i-1]){ continue; }else { count = 1; } res = Math.max(res,count); } return res; }