码迷,mamicode.com
首页 > 其他好文 > 详细

最长上升子序列

时间:2018-08-27 23:17:33      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:tps   []   ges   它的   script   ems   out   esc   turn   

package leetcode;

/**
 * 
    题目描述: 最长上升子序列
    给定一个无序的整数数组,找到其中最长上升子序列的长度。

    示例:
    
    输入: [10,9,2,5,3,7,101,18]
    输出: 4 
    解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
    
    解题思路:通过递归解决的这个问题,每个值,只需要在其前边比其小的值基础上加1即可,然后和当前的结果比较做大值。
    
    题目链接:https://leetcode-cn.com/problems/longest-increasing-subsequence/description/
 * @author woniu
 *
 */
public class LongestIncreasingSubsequence {
    public int lengthOfLIS(int[] nums) {
        int len = nums.length;
        int[] dp = new int[nums.length];
        for(int i=0; i<len; i++) {
            dp[i] = 1;
        }
        int maxLen = 0;
        for(int i=0; i<len; i++) {
            for(int j=i-1; j>=0; j--) {
                if(nums[j] < nums[i]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            
            if(maxLen < dp[i]) {
                maxLen = dp[i];
            }
        }
        
        return maxLen;
    }
    public static void main(String[] args) {
        int[] nums = {10,9,2,5,3,7,101,18};
        System.out.println(new LongestIncreasingSubsequence().lengthOfLIS(nums));
    }
}

 

最长上升子序列

标签:tps   []   ges   它的   script   ems   out   esc   turn   

原文地址:https://www.cnblogs.com/woniu4/p/9545255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!