public static void main(String[] args) {
int[] num = {1,2,3,2,3,4,5,3,4,5,6,7};
System.out.println("最长连续递增的子序列为:" + findLength(num));
}
//贪心算法,获取数组中最长递增子序列
public static int findLength(int[] num) {
int start = 0;
int max = 0;
for (int i = 1; i < num.length; i++) {
if (num[i] <= num[i - 1]){
start = i;
}
max = Math.max(i - start + 1, max);
}
return max;
}
贪心算法,获取数组中最长递增子序列
于 2023-08-22 22:06:40 首次发布
本文介绍了如何使用Java编写一个简单的贪心算法来寻找给定整数数组中的最长连续递增子序列。通过for循环和条件判断,函数`findLength`计算并返回最长递增子序列的长度。

318

被折叠的 条评论
为什么被折叠?



