2017-04-03 26 views
0

例如, 鑑於[100,4,200,1,3,2], 最長的連續元素序列是[1,2,3,4]。寫程序,找出整數數組最長連續序列的長度是多少?

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

    int a[]={10,15,1,2,3,4,5,11,12}; 

    int b=1; 
    int c=0; 
    for(int i=0;i<a.length-1;i++){ 

     if(a[i]-a[i+1]==-1){ 

     b=b+1; 
     c=c+1; 
     if(b>=c) 
     { 

     System.out.println(a[i]); 

     } 
     else{ 
      b=0; 
     } 

    } 

} 
} 
} 

但我正在逐漸爲1 2 3 4 11 輸出而輸出應爲1 2 3 4 5

如何獲得所需的輸出,什麼錯誤的代碼?

+2

你有沒有考慮第一排數組意見? –

+0

你必須首先使用Array類'Array.sort(a);'排序數組,或者你可以使用氣泡算法 – abcOfJavaAndCPP

+0

創建一個for循環,請格式化你的代碼。閱讀很痛苦。 – davidxxx

回答

1

你可以試試這個:

int[] array = {10,15,1,2,3,4,5,11,12}; 
List<Integer> tempList = new ArrayList<>(); // prepare temp list for later use 
List<List<Integer>> arrays = new ArrayList<>(); // used to store the sequences 
int lastNum = array[0]; // get the fist number for compasion in loop 
tempList.add(lastNum); 
for (int i = 1; i < array.length; i++) { 
    if (array[i]-1 == lastNum) { // check for sequence (e.g fist num was 12, 
     // current number is 13, so 13-1 = 12, 
     // so it has the sequence), then store the number 
     tempList.add(array[i]); // store it to the temp list 
     lastNum = array[i]; // keep current number for the next 
    } else { // if it has not the sequence, start the new sequence 
     arrays.add(tempList); // fist store the last sequence 
     tempList = new ArrayList() // clear for the next sequence 
     lastNum = array[i]; // init the lastNumnber 
     tempList.add(lastNum); 
    } 
} 
// now iterate for the longest array 
// craete an empty array to store the longest 
List<Integer> longestLength = new ArrayList<>(); 
for (List<Integer> arr : arrays) { 
    if (arr.size() > longestLength.size()) { 
     // check if the current array hase the longest size than the last one 
     // if yes, update the last one 
     longestLength = arr; 
    } 
} 
// at the end print the result. 
System.out.println("longestLength = " + longestLength); 

結果:

longestLength = [1, 2, 3, 4, 5] 
+0

實際上我沒有得到你的邏輯 –

+0

@SainathPawar現在的邏輯可以明確爲您服務!覈實! –

+1

錯誤的答案。你不必排序數組,子序列在未排序的數組中找到 –

0

如果你先排序數組,你的代碼應該可以工作。你嘗試過嗎?

+0

readable.Thanks其數組不是一個ArrayList –

+0

你仍然可以對它進行排序 進口java.util.array – Joseph

+0

排序陣列將給出錯誤的結果 –

0
import java.util.*; 
    Arrays.sort(a); 

然後

if(a[i]+1==a[i+1]) 
      //print it 
    else 
     { 
      System.out.print(a[i]); 
      i=a.length; 
     }//stop the loop 
+0

如果我使用你所提供我得到輸出1 2 3 4 ...溶液而需要的輸出爲1 2 3 4 5 –

+0

只需添加是System.out.print(A [1]);在else語句中包含最後一個因爲你結束循環的最後一個[i] +1將是6,所以它不等於10然後在else中包含最後一個順序整數 – abcOfJavaAndCPP

+0

只能在這種情況下工作,不在一般情況下 –

1

試試這個代碼

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

     int a[]={10,15,1,2,3,4,5,11,12}; 

     int ms=0;     // starting point of max subseq 
     int me=0;     //ending point of max subseq 
     int cs=0,ce=0;    //starting and ending point of current subseq 
     int max=0,c=0;   // length of max and current subseq 
     for(int i=0;i<a.length-1;i++){ 

      if(a[i]-a[i+1]==-1){ 

      if(c==0)    //we found the first element of a subseq 
      { 
       cs=i;ce=i+1;c=2; //made the starting of currrent seq=i, end=i+1 and length=2 
       } 
      else    // element is a part of subsequence but not first elem 
       { 
       ce=i+1;c++;  // increased current ending point 
       } 

       if(c>max)   // if lenth of current subseq is now largest then update staring and ending points of max 
       { 
        max=c; 
        ms=cs; 
        me=ce; 
       } 
      } 
      else    // subseq ended 
      { 
      cs=0; 
      ce=0; 
      c=0; 
       } 
     } 
     for(i=ms;i<=me;i++)   //printing max subsequence 
     System.out.println(a[i]); 
    } 
    } 

注:見描述

+0

請你解釋一下你的邏輯..這將是很大的幫助 –

+0

你肯定,等一分鐘 –

+0

@SainathPawar更新了答案 –

0
class Test 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     int a[]={10,15,1,2,3,4,5,11,12}; 
     Arrays.sort(a); 
     ArrayList<Integer>output = new ArrayList<Integer>(); 
     ArrayList<Integer>temp = new ArrayList<Integer>(); 
     for(int i =1; i<a.length; i++){ 
      //If elements have difference of one, add them to temp Arraylist 
      if(a[i-1] + 1 == a[i]){ 
       temp.add(a[i-1]); 
      } 
      else{ 
       //Add the last consecutive element 
       temp.add(a[i-1]); 

       //If temp is lager then output 
       if(temp.size() > output.size()){ 
        output = (ArrayList<Integer>) temp.clone(); 
         temp.clear(); 
       }   
      } 
     } 

     //Outside for loop, making sure the output is the longer list. This is to handle the case where the consecutive sequence is towards the end of the array 
     if(temp.size() > output.size()){ 
      output = (ArrayList<Integer>) temp.clone();System.out.println("after clone outside for " + output.toString()); 
     } 
     System.out.println(output.toString()); 
    } 

} 
+0

,它給出了O/P爲15 1 2 3 4 5.我們需要1 2 3 4 5 –

+0

我得到[1,2,3,4,5]作爲輸出。您正在測試的輸入是什麼? – SRm