March punch card activity on day 14 of 300 LeetCode question: rise longest sequence (medium)

March punch card activity on day 14 of 300 LeetCode question: rise longest sequence (medium)

  • Title: Given a disordered array of integers, wherein the length of the longest found rising sequence.
    Here Insert Picture Description
  • Problem-solving ideas: you have not seen, and sent four not want to change, and who are not big weekend of rest that way. . .
class Solution {
    public int lengthOfLIS(int[] nums) {
        int max = 0;
        int len = nums.length;
        if(len==0) return 0;
        if(len==1) return 1;
        int now = 0;
        int left = 0;
        int right = 0;
        while(right<len){
            if(nums[left]<nums[right]){
                max++;
                now = nums[left];
                left++;
                right++;
            }else if(nums[left]>=nums[right]){
                if(nums[right]>now && now!=0){
                    now = nums[right];
                }else if(nums[right]<now){
                    now = nums[right];
                    max = 0;
                }
                left = right;
                right++;
            }
        }
        return max+1;
    }
}

Here Insert Picture Description

  • Problem solution practice 1: turn to find himself in front is smaller than the number of numbers.
public class Solution {
    public int lengthOfLIS(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int[] dp = new int[nums.length];
        dp[0] = 1;
        int maxans = 1;
        for (int i = 1; i < dp.length; i++) {
            int maxval = 0;
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    maxval = Math.max(maxval, dp[j]);
                }
            }
            dp[i] = maxval + 1;
            maxans = Math.max(maxans, dp[i]);
        }
        return maxans;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/zui-chang-shang-sheng-zi-xu-lie-by-leetcode-soluti/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Here Insert Picture Description

Published 100 original articles · won praise 12 · views 2354

Guess you like

Origin blog.csdn.net/new_whiter/article/details/104856247