编程之美_012求数组中最长递增子序列
来源:互联网 发布:淘宝类目宝贝数量查询 编辑:程序博客网 时间:2024/05/18 21:49
package com.wzs;import java.util.ArrayList;import java.util.Arrays;import java.util.List;//求数组中最长递增子序列public class Test{ public static void main(String[] args) { int a[] = { 1, -1, 2, -3, 4, -5, 6, -7 }; find1(a); } // [1, -1, 2, -3, 4, -5, 6, -7] // [1, 1, 2, 1, 3, 1, 4, 1] // 时间复杂度:O(N*N) public static void find1(int[] a) { int length = a.length; int[] list = new int[length];// 存储第i个元素之前的最长递增序列值 List<Integer> result = new ArrayList<Integer>(); // 存储最长递增序列 for (int i = 0; i < length; i++) { list[i] = 1; for (int j = 0; j < i; j++) { if (a[j] < a[i] && list[j] + 1 > list[i]) { list[i] = list[j] + 1; if (result.isEmpty()) { result.add(list[j]); } if (!result.contains(list[i])) { result.add(list[i]); } } } } System.out.println("第i个元素时最长递增序列:" + Arrays.toString(list)); // 寻找list中最大值 int max = list[0]; for (int i = 0; i < length; i++) { if (list[i] > max) { max = list[i]; } } System.out.println("最长递增序列长度:" + max); System.out.println("最长递增序列:" + result); }}
输出结果:
第i个元素时最长递增序列:[1, 1, 2, 1, 3, 1, 4, 1]最长递增序列长度:4最长递增序列:[1, 2, 3, 4]
- 编程之美_012求数组中最长递增子序列
- [编程之美]求数组中最长递增子序列
- 编程之美--求数组中最长递增子序列
- 编程之美--求数组中最长递增子序列
- 编程之美 求数组中的最长递增子序列
- 读书笔记之编程之美 - 2.16 求数组中最长递增子序列
- 编程之美读书笔记之2.16求数组中最长递增子序列
- 编程之美: 第二章 数字之魅 2.16求数组中最长递增子序列
- 编程之美读书笔记_2.16 求数组中最长递增子序列
- 《编程之美》读书笔记17: 2.16 求数组中最长递增子序列
- 编程之美2.16 求数组中最长递增子序列
- 编程之美2.16——求数组中最长递增子序列
- 编程之美2.16求数组中最长递增子序列
- 求数组中最长递增子序列—动态规划入门(编程之美)
- POJ 2533 Longest Ordered Subsequence 编程之美 2.16 求数组中最长递增子序列
- 编程之美---求数组中最长递增子序列LIS
- 编程之美 2.16 求数组中最长递增子序列
- 【编程之美】2.16 求数组中最长递增子序列
- 线程池的使用
- Android开发教程 --- 搭建Java版WebService
- flex 权限控制
- Mondrian聚合和存储策略
- 【Hadoop】利用MultipleOutputs,MultiOutputFormat实现以不同格式输出到多个文件
- 编程之美_012求数组中最长递增子序列
- 图像处理网站
- 在对话框中动态创建的菜单不能响应UpdeateCommand的问题解决方法
- 通用读取配置文件的代码
- java 域验证用户
- 我们在囧途之程序员做私活小记
- java 获取网页源代码
- Introduction to the AndEngine
- socket从内核角度讲一次最大允许读取多少字节