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

huangapple go评论75阅读模式
英文:

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

问题

以下是代码的翻译部分:

我在尝试查找序列的最长连续元素时遇到了一些问题而这部分是对数组进行排序并将连续元素的长度添加到 ArrayList 中的部分有人能告诉我为什么这段代码是错误的吗

public static int longest_sequence(int[] array)
{
    int length = 0;
    // 用于保存所有长度的 ArrayList
    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);
}
英文:

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. 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&lt;Integer&gt; consecutiveArray = new ArrayList&lt;Integer&gt;();
	
	Arrays.sort(array);
	
	for (int i = 0; i &lt; 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);
}

答案1

得分: 0

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;
}
英文:
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 &lt; 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.
  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.

huangapple
  • 本文由 发表于 2020年8月4日 02:29:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63234988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定