先排序, 再从有序数组中找连续
import java.util.*;
public class Solution {
public int MLS (int[] arr) {
Arrays.sort(arr);
int curLen = 0;
int maxLen = 0;
int last = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == last) continue; // skip duplicates
if (arr[i] == last + 1) {
curLen++;
maxLen = Math.max(maxLen, curLen);
} else {
curLen = 1;
}
last = arr[i];
}
return maxLen;
}
}