• 300. 最长上升子序列


    给定一个无序的整数数组,找到其中最长上升子序列的长度。

    示例:

    输入: [10,9,2,5,3,7,101,18]
    输出: 4 
    解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。

    说明:

    • 可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。
    • 你算法的时间复杂度应该为 O(n2) 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/longest-increasing-subsequence


    思路:

    每个位置的数字都和他前面的数字比较一下,用dp数组存储有哪些比他小的数,就是可能组成的最长子序列

    后面再比较的时候就在前面的数字已经有的子序列长度基础上加一,就是加上自己

    class Solution {
        public int lengthOfLIS(int[] nums) {
            int len = nums.length;
            int [] dp = new int[len];
            for (int i = 0; i < len; i++) {
                int max = 1;
                for (int j = 0; j < i; j++) {
                    if (nums[i] > nums[j]){
                        max = Math.max(max,dp[j] + 1);
                    }
                }
                dp[i] = max;
            }
            return find_max(dp);
        }
        public int find_max(int [] dp){
            int res = 0;
            for (int i = 0; i < dp.length; i++) {
                res = Math.max(res, dp[i]);
            }
            return res;
        }
    }
  • 相关阅读:
    Apache虚拟主机(VirtualHost)配置
    LAMP源码安装
    SUSE上配置SAMBA服务
    Linux下安装或升级Python 2.7
    HTML5,CSS3,JS绘制饼图
    Single Number
    Reverse Words in a String
    C++控制台日历
    百度JS破盗链
    腾讯前端突击队Ⅱ
  • 原文地址:https://www.cnblogs.com/zzxisgod/p/13415179.html
Copyright © 2020-2023  润新知