Today I encountered a problem of finding the longest incremental sub-sequence. After reading it, I tried to implement it in Java. I will not elaborate on what is the longest incremental sub-sequence here. You can use it on Baidu or Google. The following is the implementation code:
Note: The functions implemented in this section of code are
(1) Randomly generate an array with 10 elements, and then output its longest incremental subsequence (2) Output the length of the longest incremental subsequence ending with one of the elements as the length
The specific implementation ideas have been shown in detail in the comments, which are relatively simple, so I will not repeat them here.
import java.util.Arrays;import java.util.Random;public class LIS { public static void main(String[] args){ System.out.println("generating a random array..."); LIS lis=new LIS(); int[] oldArray=lis.randomArray(); for (int i = 0; i < oldArray.length; i++) { System.out.print(oldArray[i]+" "); } System.out.println(); System.out.println("The length of the longest incremental subsequence is"); lis.lisGet(oldArray); } public int[] randomArray(){ Random random=new Random(); int[] randomArray=new int[10]; for (int i = 0; i < 10; i++) { randomArray[i]=random.nextInt(10); } return randomArray; } public void lisGet(int[] arrayL ){ int[] lisLength=new int[arrayL.length];//The length of the longest incremental sequence used to record the current element as the largest element for (int i = 0; i < arrayL.length; i++) { //Initialize lisLength[i]=1; } int max=1; for (int i = 1; i < arrayL.length; i++) { for (int j = 0; j <i; j++) { if (arrayL[j]<arrayL[i]&&(lisLength[j]+1)>lisLength[i]) { lisLength[i]=lisLength[j]+1; } if (max<lisLength[i]) { //Get the length of the current longest incremental sequence and the position of the last element of the subsequence max=lisLength[i]; } } } System.out.println(max); System.out.println("The longest incremental subsequence at the end of the i-th element: "+Arrays.toString(lisLength)); //Output array}}The above is the entire content of the simple implementation of the longest incremental sub-sequence Java that the editor brings to you. I hope it will be helpful to you and support Wulin.com more~