给你一个排序的数组,例如 { 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, 15 } ,求最长连续子序列的长度
public static int getLongest(int[] strs) {
int max = 0;
int oldmax = 0;
for (int i = 1; i < strs.length; ++i) {
if (strs[i] - strs[i - 1] == 1) {
max = max + 1;
oldmax = max;
} else {
max = 0;
}
}
return Math.max(max, oldmax);
}
时间复杂度应该是 O(n)
如果变形呢?给你一个没有排序的。
- 先对数组进行排序,然后再进行如上操作
- 结合 hash 表
public static int getLongestV2(int[] strs) {
Map<Integer, Integer> map = new HashMap<>();
int max = 0;
for (int str : strs) {
// 0 表示没有处理过
if (map.getOrDefault(str, 0) == 0) {
int left = map.getOrDefault(str - 1, 0); // 左序列长度
int right = map.getOrDefault(str + 1, 0); // 右序列长度
map.put(str, right + left + 1);
// 设置左端点
if (left != 0) {
map.put(str - left, left + right + 1);
}
// 设置右端点
if (right != 0) {
map.put(str + right, right + left + 1);
}
max = max > (left + right + 1) ? max : (left + right + 1);
}
}
return max;
}
注意:在更新两端节点的序列长度时,也要更新当前节点的序列长度。