简体   繁体   English

从给定的未排序整数数组中查找最长连续元素序列的长度

[英]Find the length of the longest consecutive elements sequence from a given unsorted array of integers

I am having some trouble trying to find the longest consecutive elements of a sequence, and this part is the one that sorts the array and adds the length of the consecutive elements in an ArrayList.我在寻找序列中最长的连续元素时遇到了一些麻烦,这部分是对数组进行排序并在 ArrayList 中添加连续元素长度的部分。 Could someone tell me why this code is wrong?有人能告诉我为什么这段代码是错误的吗?

public static int longest_sequence(int[] array)
{
    int length = 0;
    // ArrayList to hold all lengths
    ArrayList<Integer> consecutiveArray = new ArrayList<Integer>();
    
    Arrays.sort(array);
    
    for (int i = 0; i < array.length - 1; i++)
    {
        if (array[i] + 1 == array[i + 1])
        {
            length++;
        }
        else
        {
            consecutiveArray.add(length);
            length = 0;
        }
    }

    Collections.sort(consecutiveArray);
    
    return consecutiveArray.get(consecutiveArray.size() - 1);
}
public static int longest_sequence(int[] array) {
    if (array.length == 0)
        return 0;

    int length = 1;
    int ans = 1;
    
    Arrays.sort(array);
    
    for (int i = 0; i < array.length - 1; i++)
    {
        if(array[i] == array[i+1]) {
            continue;
        }
        else if (array[i] + 1 == array[i + 1])
        {
            length++;
        }
        else
        {
            length = 1;
        }
        
        ans = Math.max(ans,length);
    }

    
    return ans;
}

Ok, assuming longest consecutive sequence isnt has to be ordered, you can do few things:好的,假设不必订购最长的连续序列,您可以做几件事:

  1. Length can always be one because longest consecutive sequence would always contain a single number, unless the input array is empty.长度总是可以是一,因为最长的连续序列总是包含一个数字,除非输入数组为空。
  2. You need to skip equal consecutive numbers, which the first if does by continuing the iteration.您需要跳过相等的连续数字,第一个 if 通过继续迭代来完成。
  3. You don't need the consecutive_array, a single variable would be sufficient to hold the length of longest consecutive sequence, which the "ans" variable does.您不需要连续数组,单个变量足以保存最长连续序列的长度,“ans”变量就是这样做的。

暂无
暂无

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

相关问题 编写一个程序来查找整数数组中最长的连续序列的长度? - Write a program to find length of longest consecutive sequence in array of integers? 如何从Java中的数组中提取最长的连续整数序列? - How to extract longest consecutive sequence of integers from array in Java? 在整数数组中找到最长连续序列的长度的程序的时间复杂度是多少? - What is the time-complexity of a program that finds the length of the longest consecutive sequence in an array of integers? Java - 查找最长递增连续元素序列的程序(逻辑错误) - Java - Program to find longest increasing sequence of consecutive elements (logical errors) 给定一个由 n 个整数组成的未排序数组 𝐴 和一个整数 x,重新排列 𝐴 中的元素 - Given an unsorted array 𝐴 of n integers and an integer x, rearrange the elements in 𝐴 java 方法来找到与给定编号最近的匹配。 在未排序的整数数组中 - java method to find nearest match to a given no. in unsorted array of integers [java]最长连续整数序列,调试 - [java]Longest consecutive integers sequence, debug 优化此解决方案:最长连续的不同整数序列 - Optimizing this solution: Longest consecutive distinct sequence of integers 在给定整数数组的情况下查找所有增长最长的子序列 - 动态编程 - To find all longest increasing subsequences given an array of integers - Dynamic Programming 如何找到连续自然继承者的最长序列? - How to find the longest sequence of consecutive natural successors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM