@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); } } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)