如何在整数数组中查找整数的重复序列?

如何在整数数组中查找整数的重复序列?,第1张

如何在整数数组中查找整数的重复序列

@MiljenMikic的答案很好,尤其是因为语法实际上不是常规的。:D

如果您想一般地在一个数组上进行 *** 作,或者想了解它,那么可以做到正则表达式几乎完全可以做到:

public static void main(String[] args) {    int[] arr = {0, 1, 2, 3, 2, 3}; // 2, 3 repeats at position 2.    // for every position in the array:    for (int startPos = 0; startPos < arr.length; startPos++) {        // check if there is a repeating sequence here:        // check every sequence length which is lower or equal to half the        // remaining array length: (this is important, otherwise we'll go out of bounds)        for (int sequenceLength = 1; sequenceLength <= (arr.length - startPos) / 2; sequenceLength++) { // check if the sequences of length sequenceLength which start // at startPos and (startPos + sequenceLength (the one // immediately following it)) are equal: boolean sequencesAreEqual = true; for (int i = 0; i < sequenceLength; i++) {     if (arr[startPos + i] != arr[startPos + sequenceLength + i]) {         sequencesAreEqual = false;         break;     } } if (sequencesAreEqual) {     System.out.println("Found repeating sequence at pos " + startPos); }        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5600456.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存