p115 最长递增子序列的长度(leetcode 300)

一:解题思路

方法一:用动态规划的思想来做。Time:O(n^2),Space:O(n)

方法二:采用二分搜索的思想来做。Time:O(n*log(n)),Space:O(n)

二:完整代码示例 (C++版和Java版)

方法一C++:

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) 
    {
        if (nums.size() == 0) return 0;
        int n = nums.size();
        vector<int> d(n,0);
        d[0] = 1;
        int maxValue = 1;
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                int cur = nums[i] > nums[j] ? d[j] + 1 : 1;
                d[i] = max(d[i],cur);
            }
            maxValue = max(d[i],maxValue);
        }

        return maxValue;
    }
};

方法一Java:

class Solution 
    {
        public int lengthOfLIS(int[] nums) 
        {
               if(nums==null || nums.length==0) return 0;
               int n=nums.length;
               int[] d=new int[n];
               d[0]=1;
               int maxValue=1;
               for(int i=1;i<n;i++)
               {
                   for(int j=0;j<i;j++)
                   {
                       int cur=nums[i]>nums[j]?d[j]+1:1;
                       d[i]=Math.max(cur,d[i]);
                   }
                   maxValue=Math.max(maxValue,d[i]);
               }
               
               return maxValue;
        }
    }

方法二C++:

class Solution 
{
private:
    int binarySeachInsertPosition(vector<int>& nums, int len, int x)
    {
        int low = 0, high = len - 1;
        while (low <= high)
        {
            int mid = low + (high-low) / 2;
            if (nums[mid] == x) return mid;
            else if (nums[mid] > x) high = mid - 1;
            else low = mid + 1;
        }

        return low;
    }
public:
    int lengthOfLIS(vector<int>& nums) 
    {
        if (nums.size() == 0) return 0;
        int n = nums.size();
        vector<int> d(n,0);
        int len = 0;
        for (int x : nums)
        {
            int i = binarySeachInsertPosition(d,len,x);
            d[i] = x;
            if (i == len) len++;
        }

        return len;
    }
};

方法二Java:

class Solution
    {
        private int binarySearchInsertPosition(int[] nums,int len,int x)
        {
            int low=0,high=len-1;
            while(low<=high)
            {
                int mid=low+(high-low)/2;
                if(nums[mid]==x) return mid;
                else if(nums[mid]>x) high=mid-1;
                else low=mid+1;
            }
            
            return low;
        }
        
        public int lengthOfLIS(int[] nums) 
        {
               if(nums==null || nums.length==0) return 0;
               int n=nums.length;
               int[] d=new int[n];
               int len=0;
               for(int x:nums)
               {
                   int i=binarySearchInsertPosition(d,len,x);
                   d[i]=x;
                   if(i==len) len++;
               }
               
               return len;
        }
    }

 

上一篇:Handlebars模板引擎之进阶


下一篇:p126 跳跃游戏(leetcode 55)