2015-02-23 120 views
5

我的任務是編寫一個程序,該程序在給定的數組中找到最長的連續子序列,並打印該子序列的長度以及它自身的子序列。 說陣列是:查找數組中最長的連續子序列

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); 
    } 
} 

所以我想通了如何讓程序識別最長的子序列的長度。但是,我堅持要如何才能真正將它打印出來。現在,我只是試圖讓方法正確地打印出陣列中最長的子序列開始和結束的地方。這不是程序中需要的,但我認爲在開始打印之前我需要弄清楚這一點。

我推斷,爲了打印子序列,我需要跟蹤最長序列何時開始和結束,並從那裏獲得打印在這些元素上的程序。但我的代碼似乎沒有正確運行。沒有給出的錯誤,它只是運行,但不返回任何東西。

任何幫助,非常感謝。謝謝!

+0

答案爲什麼不只是創建一個返回的最長連續子序列(或如果超過一個最大長度連續子存在一個這樣子)的功能?然後,你可以執行'subSequence.length'(或其他)來獲得它的長度。 – 2015-02-23 17:53:49

+0

只是實現相同事情的另一種方式。看看你是否有任何提示:http://www.sanfoundry.com/java-program-implement-longest-arithmetic-progression-algorithm/ – CKing 2015-02-23 17:55:07

+0

我不確定你的意思是「創建一個函數」。你的意思是創建一個幫助器方法,返回正確的數組並將其傳遞給我的longestForward方法,然後將其傳遞給我的主方法?如果是這樣,我會怎麼做呢?我已經嘗試了很多代碼迭代來追蹤我想要的內容,但我無法弄清楚。我可以開始嗎? – Zephyr 2015-02-23 17:59:00

回答

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] + ", ");   
} 
+0

哇謝謝你!它完美地打印出最長的陣列。我加了一點,以使它適合任務,但它是正確的。謝謝! – Zephyr 2015-02-23 18:08:51

+0

@Zephyr不客氣。 – 2015-02-23 18:09:32

+0

@ Jean-FrançoisSavard如果序列的組成部分不是由1算術增長,該怎麼辦? – zubergu 2015-02-23 18:34:42

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); 
+0

啊。發現得好。代碼現在運行。不幸的是,它沒有爲任何事物返回正確的值。當我註釋掉其他索引,如果循環,它會返回正確的長度值(但不是正確的indexStart和End值)。仍然卡住。 – Zephyr 2015-02-23 18:02:27

+0

@ Zephyr檢查更新。 – zubergu 2015-02-23 18:05:17

+0

在Jean-Francois的回答後,我比較了他的其他邏輯與我的關係。我想我發現了我的錯誤。我用布爾錯誤,並且我的代碼不規則地跟蹤結束,而不是每次迭代都遞增。感謝您的有用評論。 – Zephyr 2015-02-23 18:10:59

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; 

} 
相關問題