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

Java 如何在整数数组中找到重复的整数序列?,java,arrays,algorithm,math,Java,Arrays,Algorithm,Math,如何在整数数组中找到重复的整数序列 00会重复,123123也会重复,但01234593623不会 我有一个如何做到这一点的想法,但它在我的脑海中是模糊的,我的实现没有走远,由于这一点 我的想法是 每次通过for循环时都会偏移一定量 循环遍历它的内部,并按偏移量比较数字块 在Java中,我做到了这一点: String[] p1 = new String[nDigitGroup]; String[] p2 = new String[nDigitGroup]; for (i

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

00会重复,123123也会重复,但01234593623不会

我有一个如何做到这一点的想法,但它在我的脑海中是模糊的,我的实现没有走远,由于这一点

我的想法是

  • 每次通过for循环时都会偏移一定量
  • 循环遍历它的内部,并按偏移量比较数字块
  • 在Java中,我做到了这一点:

        String[] p1 = new String[nDigitGroup];
        String[] p2 = new String[nDigitGroup];
    
        for (int pos = 0; pos < number.length - 1; pos++)
        {
            System.out.println("HERE: " + pos + (nDigitGroup - 1));
            int arrayCounter = -1;
    
            for (int n = pos; n < pos + nDigitGroup ; n++)
            {
                System.out.printf("\nPOS: %d\nN: %d\n", pos, n);
                arrayCounter++;
                p1[arrayCounter] = number[n];
    
                System.out.println(p1[arrayCounter]);
            }
    
            pos += nDigitGroup;
            arrayCounter = -1;
    
            System.out.println("SWITCHING");
    
            for (int n = pos; n < pos + nDigitGroup ; n++)
            {
                System.out.printf("\nPOS: %d\nN: %d\n", pos, n);
                arrayCounter++;
                p2[arrayCounter] = number[n];
    
                System.out.println(p2[arrayCounter]);
            }
    
            if (p1[0].equals(p2[0]) && p1[1].equals(p2[1])) System.out.println("MATCHING");
        }
    
    我正确地填充了节数组,但它在索引越界异常时中断。

    尝试以下操作:

    string lookIn = "99123998877665544123"; 
    // above has length of 20 (in positions 0 through 19)
    int patternLength = 3;
    // want to search each triple of letters 0-2, 1-3, 2-4 ... 17-19
    //   however since there must be 3 chars after the 3-char pattern
    //   we only want to search the triples up to 14-16 (20 - 3*2)
    for (int i=0; i <= lookIn.Length - patternLength * 2; i++) {
       string lookingFor = lookIn.Substring(i, patternLength);
       // start looking at the pos after the pattern
       int iFoundPos = lookIn.IndexOf(lookingFor, i + patternLength);
       if (iFoundPos > -1) {
          string msg = "Found pattern '" + lookingFor 
                     + "' at position " + i 
                     + " recurs at position " + iFoundPos;
       }
    }
    // of course, you will want to validate that patternLength is less than
    //   or equal to half the length of lookIn.Length, etc.
    

    您可以始终使用正则表达式来获得所需的结果。使用并将其与贪婪量词组合:

        void printRepeating(String arrayOfInt)
        {
            String regex = "(\\d+)\\1";
            Pattern patt = Pattern.compile(regex);
            Matcher matcher = patt.matcher(arrayOfInt);           
            while (matcher.find())                              
            {               
                System.out.println("Repeated substring: " + matcher.group(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);
                }
            }
        }
    }
    
    publicstaticvoidmain(字符串[]args){
    int[]arr={0,1,2,3,2,3};//2,3在位置2重复。
    //对于阵列中的每个位置:
    对于(int startPos=0;startPos对于(int sequenceLength=1;sequenceLength,由@AdrianLeonhard发布的答案非常有效
    0,1,2,3,4,3,5,6,4,7,8,7,8
    许多人可能想知道如何从数组中获取所有重复的数字

    所以,我写了这个简单的逻辑,它打印出所有重复的数字及其位置

        int[] arr = {0, 1, 2, 3, 4, 3, 5, 6, 4, 7, 8, 7, 8};
    
        for(int i=0; i<arr.length;i++){
            for(int j=i+1; j<arr.length;j++){
                if(arr[i] == arr[j]){
                    System.out.println("Number: "+arr[i]+" found repeating at position: "+i+" , repeated at position "+j);
                }
            }
        }
    
    int[]arr={0,1,2,3,4,3,5,6,4,7,8,7,8};
    
    对于(int i=0;i分解:首先编写一个函数来检查字符串是否是n位的重复序列。然后循环n的可能值。数组中的每个整数是否在0-9范围内?然后我认为将其视为
    字符串
    。可以通过执行
    java.util.Arrays.toString从数组中提取一个(ints.replaceAll(“^[0-9]”,“”)
    或诸如此类。然后,您可以相互比较字符串块,从一个字符长的子字符串开始,以子字符串
    n/2个字符长的子字符串结束,其中n=整数的数量。您想查找所有重复序列还是只查找长度最大的序列,或者您只需要知道是否所有重复序列re在给定字符串中重复整数序列?3种不同的东西:)我只需要知道是否有任何部分重复。不需要识别哪些部分重复。这只适用于三个长度的模式吗?因为它还应该匹配像123412341234这样的内容。这也会在运行时出现越界异常。不,只需设置“int patternLength=3;”到任意数字。如果需要,请将我给出的整个代码循环放入(int patternLength=3;patternLength<7;patternLength++){my code}的循环中。此循环将查找大小为3、4、5、6和7的每个图案。将3和7更改为任何值。请记住,您不应使用大于正在搜索的字符串长度一半的图案长度,因为这毫无意义。例如,在11个字符的字符串中查找重复的10个字符的字符串是毫无意义的字符串长度必须为20个或更多字符。很抱歉,我用C#…而不是java…运行良好。真的很抱歉。我添加了java代码作为编辑。我测试了它。但这不是他要求的问题。你必须获得重复序列。你的程序正在查找给定字符串中给定长度的字符串。如果如果要使用字符串,请记住使用
    equals
    而不是
    =
    。此外,还可以使用
    List arrList=Arrays.asList(arr);boolean sequencesAreEqual=arrList.subList(startPos,sequenceLength)。equals(arrList.subList(startPos+sequenceLength,sequenceLength)
    两个答案我都喜欢,但这个答案让我更深入地了解它的实际工作原理,这正是我所需要的。我对两个答案都投了赞成票,但选择了这个。
        void printRepeating(String arrayOfInt)
        {
            String regex = "(\\d+)\\1";
            Pattern patt = Pattern.compile(regex);
            Matcher matcher = patt.matcher(arrayOfInt);           
            while (matcher.find())                              
            {               
                System.out.println("Repeated substring: " + matcher.group(1));
            } 
        }          
    
    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);
                }
            }
        }
    }
    
        int[] arr = {0, 1, 2, 3, 4, 3, 5, 6, 4, 7, 8, 7, 8};
    
        for(int i=0; i<arr.length;i++){
            for(int j=i+1; j<arr.length;j++){
                if(arr[i] == arr[j]){
                    System.out.println("Number: "+arr[i]+" found repeating at position: "+i+" , repeated at position "+j);
                }
            }
        }