简体   繁体   English

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

[英]How to find repeating sequence of Integers in an array of Integers in java?

For example if input is:例如,如果输入是:

2 0 6 3 1 6 3 1 6 3 1 2 0 6 3 1 6 3 1 6 3 1

then output should be 6 3 1.Need to find the first repetition cycle.那么输出应该是6 3 1.需要找到第一个重复周期。

class FindDuplicate
{
void printRepeating(int arr[], int size)
{
    int i; 
    System.out.println("The repeating elements are : ");

    for (i = 0; i < size; i++)
    {
        if (arr[Math.abs(arr[i])] >= 0)
            arr[Math.abs(arr[i])] = -arr[Math.abs(arr[i])];
        else
            System.out.print(Math.abs(arr[i]) + " ");
    }        
} 


public static void main(String[] args) 
{
    FindDuplicate duplicate = new FindDuplicate();
    int arr[] = {1, 2, 3, 1, 2, 3, 1, 2, 3 };
    int arr_size = arr.length;
    duplicate.printRepeating(arr, arr_size);
}
}

https://pastebin.com/12bnjzfw Have used java 8 streams for collection creation. https://pastebin.com/12bnjzfw已使用 java 8 流进行集合创建。

for (int seqSize = ints.size() / 2; seqSize > 0; seqSize--) { //It should be first cycle. Main priority is biggest sequence
  for (int i = 0; i < ints.size() / seqSize; i++) { //Start position of the first block
    for (int j = i + seqSize; j < ints.size() - seqSize + 1; j++) {
      if (ints.subList(i, i + seqSize).equals(ints.subList(j, j + seqSize))) {
        System.out.println("Answer is: " + ints.subList(i, i + seqSize));
        return;
      }
    }
  }
}

Loop through those array elements and cache your elements until you have the same value for example例如,循环遍历这些数组元素并缓存您的元素,直到您具有相同的值

List list = Arrays.asList(1,2,3);
if(list.contains(element))
 //code to check here

you will need get size of your list and based on that check exactly this amount of items if they match if yes then output if not clear cache and start cache from the begging.您将需要获取列表的大小,并根据该大小检查这些项目是否匹配,如果是,则输出如果不清除缓存并从请求开始缓存。

class FindDuplicate {
    static int STRING_LENGTH = 3;

    void printRepeating(int arr[], int size) {
        int i; 
        System.out.println("The repeating elements are : ");
        String strVal = "";
        for (int ii = 0; ii < size; ii++) {
            strVal += arr[ii]; 
        }
        // strVal now has something we can search with.

        for (i = 0; i < size; i++) {
            int end = Math.min(size,i+STRING_LENGTH );
            String searchString = strVal.substring(i, end);
            if (searchString.length() != STRING_LENGTH)
                break;  // at end of arr, doesn't have length to search 
            int matchIndex = strVal.indexOf(searchString, i+1);
            if (matchIndex != -1) {
               String match = strVal.substring(matchIndex, matchIndex + STRING_LENGTH);
               System.out.print(match + " ");
               break; // done with loop
            }
        }        
    } 


    public static void main(String[] args) {
        FindDuplicate duplicate = new FindDuplicate();
        int arr[] = {1, 2, 3, 1, 2, 3, 1, 2, 3 };
        int arr_size = arr.length;
        duplicate.printRepeating(arr, arr_size);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM