我的解决方法如下:
数据结构
- 连续的
sequence(我们想要找到的)是至少2连续的Integers(对)的List
- 要返回全部
foundSequences,您需要一个结果List 包含0 个或更多 Lists
- 要检查连续性,您需要
current 和previous 元素
算法
应用逻辑(如果):
-
如果
current == previous + 1,连续性被发现,否则现有的连续序列被破坏
- 如果一个序列至少有 2 个元素,即
sequence.size() > 1,则应将其添加到结果列表中(即foundSequences)
- 在 first 元素之前和每个 broken 序列之后,
previous == null
- 在 last 元素之后可能有一个带有
sequence.size() > 1 的开放序列。如果是这样,那么这个序列没有被破坏,而是完整的并且应该被添加到结果列表中(即foundSequences)
迭代元素(循环):
- 使用 for-each 循环处理(已排序!)数组的所有元素
- for循环自动填充
current元素(迭代变量)
- 您必须跟踪
previous 元素(因此在循环之前初始化null)。它必须归档每个循环的当前元素,以便我们可以将下一个元素与它进行比较。
- 除非当前元素不再连续(发生中断),否则
previous 将变为null 以开始新的收集。
- 如果出现中断,当前找到的序列可能会添加到结果中。然后需要将
sequence 重置为null 以开始新的收集。
- 检查最后一个元素并结束循环后,可能还有一个序列(尚未中断)。这需要添加到结果中。
8.
来源
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class ConsecutiveSequenceFinder {
private int[] unsortedNumbers;
public ConsecutiveSequenceFinder(int[] numbers) {
this.unsortedNumbers = numbers;
}
public int[] sorted() {
int[] sortedNumbers = Arrays.copyOf(this.unsortedNumbers, this.unsortedNumbers.length);
Arrays.sort(sortedNumbers);
return sortedNumbers;
}
public List<List<Integer>> findSequences() {
// one sequence is List of integers; thus list of sequences is list of list of integers
List<List<Integer>> foundSequences = new ArrayList<>();
// first we sort the array
int[] ascending = this.sorted();
// this working variable will hold the currently found sequence
List<Integer> sequence = new ArrayList<Integer>();
Integer previous = null;
System.out.println("Finding sequences ..");
for (int current : ascending) {
// check if current value is first or one more than (consecutive to) previous
if (previous == null || current == previous + 1) {
sequence.add(current);
previous = current;
} else {
System.out.printf("\tsequence of %d consecutive is broken at: %d\n", sequence.size(), current);
// if sequence found (at least a pair) then add
if (sequence.size() > 1) {
foundSequences.add(sequence);
}
// and finally prepare a new sequence, to collect fresh again
sequence = new ArrayList<>();
previous = null;
}
}
// if sequence left, then add
if (sequence.size() > 1) {
System.out.printf("\tsequence of %d consecutive was completed with last array element\n", sequence.size());
foundSequences.add(sequence);
}
return foundSequences;
}
public static void main (String[] args) throws java.lang.Exception {
// demo numbers
int[] values = {202,203,204,205,206, 100, 1, 3, 200, 2, 4, 201, 5};
// starting demo
System.out.println("Input: " + Arrays.toString(values));
ConsecutiveSequenceFinder finder = new ConsecutiveSequenceFinder(values);
System.out.println("Sorted: " + Arrays.toString(finder.sorted()));
List<List<Integer>> foundSequences = finder.findSequences();
System.out.println("Found sequences: " + foundSequences.size());
// print for each sequence the size and its elements
for (List<Integer> sequence : foundSequences) {
System.out.printf("\t %d elements: %s\n",sequence.size(), sequence.toString());
}
// check for each sequence if it is the longest
List<Integer> longestSequence = new ArrayList<>();
for (List<Integer> sequence : foundSequences) {
if (sequence.size() > longestSequence.size()) {
longestSequence = sequence;
}
}
System.out.printf("Longest sequence has %d elements: %s\n",longestSequence.size(), longestSequence.toString());
}
}
实际输出
Input: [202, 203, 204, 205, 206, 100, 1, 3, 200, 2, 4, 201, 5]
Sorted: [1, 2, 3, 4, 5, 100, 200, 201, 202, 203, 204, 205, 206]
Finding sequences ..
sequence of 5 consecutive is broken at: 100
sequence of 7 consecutive was completed with last array element
Found sequences: 2
5 elements: [1, 2, 3, 4, 5]
7 elements: [200, 201, 202, 203, 204, 205, 206]
Longest sequence has 7 elements: [200, 201, 202, 203, 204, 205, 206]
Process finished with exit code 0