java 算法最长连续递增子序列

给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。

输入格式:

输入第1行给出正整数n(≤10​5​​);第2行给出n个整数,其间以空格分隔。

输出格式:

在一行中输出第一次出现的最长连续递增子序列,数字之间用空格分隔,序列结尾不能有多余空格。

输入样例:

  1. 15
  2. 1 9 2 5 7 3 4 6 8 0 11 15 17 17 10

输出样例:

3 4 6 8
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
         BufferedReader sr = new BufferedReader(new InputStreamReader(System.in));
            int n=Integer.parseInt(sr.readLine());
            int[] res=new int[n];
            String s[] = sr.readLine().split(" ");
            for(int i=0;i<n;i++)
                res[i]=Integer.parseInt(s[i]);
            int max=0,count=0,ss=0,x=0,y=0;
            for(int i=0;i<n-1;i++) {
                y=i+1;//判断是否递增,是的话count++;
                if(res[i+1]>res[i]) {
                    count++;
                    if(count>max) {
                        max=count;
                        ss=x;
                    }
                }else {
                    count=0;
                    x=y;//不连续递增,则索引改变为下一个目标
                }
            }
            for(int i=ss;i<=ss+max;i++) {
                if(i==(ss+max)) {
                    System.out.print(res[i]);
                }else {
                    System.out.print(res[i]+" ");
                }
            }
    }

}

 实现二:

public class Sample {
    public static void main(String[] args) {
        int []nums = {1,3,4,5,6,7,2,8,9,10};
        for (int[] lu : findAllLowUpIndex(nums)) {
            System.out.printf("下标:%d, 上标:%d, 长度%d
", lu[0], lu[1], lu[1]-lu[0]);
        }
    }
 
    public static List<int[]> findAllLowUpIndex(int[] nums) {
        Map<Integer, int[]> map = new HashMap<>(); //map的key是长度,value是数组的上下标
        for (int i=0, j=0; i<nums.length-1; i++) {
            for (j=i+1; j<nums.length && nums[j-1]<nums[j]; j++) {
                map.put(j-i, new int[] {i, j}); //j-i就是长度,i是下标,j是上标,相同长度的返回较大的index,所以后来的index直接覆盖之前的信息
            }
        }
        return map.entrySet().stream()
                .sorted((e1,e2)->e1.getKey().compareTo(e2.getKey())) //这里是为了按长度排序
                .map(e->e.getValue()).collect(Collectors.toList()); //这里是去掉长度信息只保留上下标信息
    }
}

  实现三:

public class Test3 {
 
    public static void main(String[] args) {
        int[] nums = new int[]{5, 6, 7, 0,
                1, 2, 3, 8, 4, 5, 7, 9, 21};
        calc(nums);
    }
 
    public static void calc(int[] nums) {
        int[] max = null;
        int start = 0;
        int end = 0;
        for (int i = 1; i < nums.length; i++) {
            int pre = nums[i - 1];
            int cur = nums[i];
            if (cur > pre) {
                end = i;
            }
            if (cur <= pre || i == nums.length - 1) {
                if (max == null || max[1] - max[0] <= end - start) {
                    max = new int[]{start, end};
                }
                start = i;
            }
        }
        System.out.println(String.format("[%s,%s]", max[0], max[1]));
    }
 
}

 实现四:

public class Sample {
    public static void main(String[] args) {
        int []nums = {1,3,4,5,6,7,2,8,9,10,3,4,5,6,7,1,8,6,5};
        for (int[] lu : findAllLowUpIndex(nums)) {
            System.out.printf("y:%d=>%d
", lu[0], lu[1]);
        }
    }
   
    public static List<int[]> findAllLowUpIndex(int[] nums) {
        List<int[]> result = new ArrayList<>();
        for (int i=0, j=0; i<nums.length-1; i=j) {
            for (j=i+1; j<nums.length && nums[j-1]<nums[j]; j++);
             
            if (j-1>i) { //长度为2以上则保存结果
                result.add(new int[] {i, j-1});
            }
        }
        return result;
    }
}
原文地址:https://www.cnblogs.com/interdrp/p/13699308.html