查找数组中最长的连续子序列

时间:2015-02-23 17:46:18

标签: java arrays

我的任务是编写一个程序,找到给定数组中增长最长的连续子序列,并打印该子序列的长度和它自己的子序列。 假设数组是:

int[] arr = {3, 6, 5, 1, 9, 3, 2, 3, 4, 5, 1}

最长的连续增加子序列是2,3,4,5,长度为4。 所以这个方法的输出将是

4
2, 3, 4, 5

到目前为止,这是我的代码:

public class LongestSubsequence {
  public static void main(String[] args) {

    // Test arrays
    int[] arrC = {9, 5, 2, 3, 4, 5};
    int[] arrA = {1, 2, 3, 4, 5, 7};
    int[] arrB = {7, 6, 5, 4, 1, 2};
    int[] arr = {3, 6, 5, 1, 9, 3, 2, 3, 4, 5, 1};

    longestForward(arr);

  }

  // input of the int array, returns nothing.
  public static void longestForward(int[] arr) {
    // variables for Length of longest subsequence found and for the length of the current sequence
    int subSeqLength = 1;
    int longest = 1;
    boolean longestSub = false;
    int indexStart = 0;
    int indexEnd = 0;

    for (int i = 0; i < arr.length-1; i++) {
      //Increases subsequence length variable 
      if (arr[i] < arr[i+1]) {
        subSeqLength++;
      }
      // Sets the current subsequence to the longest variable if it is the longest one found at the time.
      else if (subSeqLength > longest) {
        longest = subSeqLength;
        longestSub = true;
      }
      // if the current sequence being analyzed is the longest one, keeps track of where it starts and ends
      else if (longestSub = true) {
        arr[i] = indexStart;
        arr[i+1] = indexEnd;
      }
      // sets the subsequence length back to one if it is no longer increasing         
      else subSeqLength = 1;
    }

    System.out.println(subSeqLength);
    System.out.println(indexStart);
    System.out.print(indexEnd);
  }
}

所以我想出了如何让程序识别最长子序列的长度。但是,我仍然坚持如何实际打印它。现在,我只是试图让方法正确地打印最长子序列开始和结束的数组中的位置。这不是程序中需要的东西,但我认为在打印之前我需要弄明白这一点。

我推断要打印子序列,我需要跟踪最长序列何时开始和结束,并从那里得到程序在这些元素上打印。但我的代码似乎没有正确运行。没有给出错误,它只是运行但不返回任何内容。

非常感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:5)

在这里,我用注释修复了你的算法:

public static void longestForward(int[] arr)
{
    int subSeqLength = 1;
    int longest = 1;
    int indexStart = 0;
    int indexEnd = 0;

    for (int i = 0; i < arr.length - 1; i++)
    {
        if (arr[i] == arr[i + 1] - 1)//We need to check if the current is equal to the next
        {
            subSeqLength++;//if it is we increment
            if (subSeqLength > longest)//we assign the longest and new bounds
            {
                longest = subSeqLength;
                indexStart = i + 2 - subSeqLength;//make sure the index start is correct
                indexEnd = i + 2;
            }

        } 
        else
            subSeqLength = 1;//else re-initiate the straight length
    }


    for (int i = indexStart; i < indexEnd; i++)//print the sequence
        System.out.println(arr[i] + ", ");        
}

答案 1 :(得分:1)

arr[i] = indexStart;
arr[i+1] = indexEnd;

你想让它走另一条路,赋值算子从右到左分配值,但你可能已经知道了。 但这不是最大的问题,你的代码错了,无法给你正确答案,而且还有一些问题。

首先,前面提到的indexStart和indexEnd。您希望存储数组的索引,但实际上是尝试在这些单元格中存储值。

此外,每次子序列长度增加时,都应该跟踪你的子序列结束。您if/else if逻辑是错误的,您必须改进它。当你进入它时,isLongest在真实一次之后永远不会是假的,这是不好的。您需要检查这是否是最长的子序列,只有当它结束时,所以当您第一个if为假时。

for (int i = 0; i < arr.length-1; i++) {
      if (arr[i] < arr[i+1]) {
        subSeqLength++;
      } else {
        if ( subSeqLength > longest ) {
        indexStart = i - subSeqLength + 1;
        longest = subSeqLength;
        }
        subSeqLength = 1;
      }
    }

    System.out.println(longest);
    System.out.println(indexStart);
    System.out.println(indexStart + longest-1);

答案 2 :(得分:0)

这是我在JS中的答案

function longestContigousSubsequence(seq) {

    // to pointers to point at the current and next
    var count; 
    var j;
    var i = 0;
    var result = 0;
    var temp = [];
    var final = []

    // if array length is 1, return the empty array

    while(i < seq.length - 1){
        count = 0;
        j = i + 1;

        temp.push(seq[i])
        while(seq[i] < seq[j] && j < seq.length){
            count += 1;  
            temp.push(seq[j])
            j += 1
            i += 1      
        }

        if(count > result){
            result = count;
            final = temp;
        }

        i += 1;
        temp = [];
        //console.log(i + " " + count);
    }
    return final;

}