关于longest 的一些题目

Longest Common Subsequence

Given two strings, find the longest common subsequence (LCS).

Your code should return the length of LCS.

Have you met this question in a real interview?  
Yes
Example

For "ABCD" and "EDCA", the LCS is "A" (or "D""C"), return 1.

For "ABCD" and "EACB", the LCS is "AC", return 2.

class Solution {
public:
    /**
     * @param A, B: Two strings.
     * @return: The length of longest common subsequence of A and B.
     */
    int longestCommonSubsequence(string A, string B) {
        // write your code here
        int m=A.size();
        int n=B.size();
        vector<vector<int>> ma(m+1,vector<int>(n+1,0));
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(A[i-1]==B[j-1]){
                    ma[i][j]=ma[i-1][j-1]+1;
                }
                else{
                    ma[i][j]=max(ma[i][j-1],ma[i-1][j]);
                }
            }
        }
        return ma[m][n];
    }
};

Longest Increasing Subsequence

Given a sequence of integers, find the longest increasing subsequence (LIS).

You code should return the length of the LIS.

Example

For [5, 4, 1, 2, 3], the LIS  is [1, 2, 3], return 3

For [4, 2, 4, 5, 3, 7], the LIS is [4, 4, 5, 7], return 4

Challenge

Time complexity O(n^2) or O(nlogn)


class Solution {
public:
    int longestCommonSubsequence(vector<int>& A, vector<int>& B) {
        // write your code here
        int m=A.size();
        int n=B.size();
        vector<vector<int>> ma(m+1,vector<int>(n+1,0));
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(A[i-1]==B[j-1]){
                    ma[i][j]=ma[i-1][j-1]+1;
                }
                else{
                    ma[i][j]=max(ma[i][j-1],ma[i-1][j]);
                }
            }
        }
        return ma[m][n];
    }
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    int longestIncreasingSubsequence(vector<int> nums) {
        // write your code here
       // vector<int>num2(nums.begin(),nums.end());
        //sort(num2.begin(),num2.end());
        //return longestCommonSubsequence(nums,num2);
        int size=nums.size();
        vector<int> v(size,INT_MAX);
        int cnum,len=0;
        for(int i=0;i<size;i++){
            cnum=nums[i];
            //upper_bound return the first element bigger than the value
            //in the 1,1,1,1 case  it can work
            auto it=upper_bound(v.begin(),v.begin()+len+1,cnum);
            *it=cnum;
            len=max(len,(int)(it-v.begin())+1);
        }
        return len;
    }
};

Longest Common Prefix
Given k strings, find the longest common prefix ( LCP ).


Longest Common Substring

Given two strings, find the longest common substring.

Return the length of it.


class Solution {
public:    
    /**
     * @param A, B: Two string.
     * @return: the length of the longest common substring.
     */
    int longestCommonSubstring(string &A, string &B) {
        // write your code here
        // write your code here
        int m=A.size();
        int n=B.size();
        vector<vector<int>> ma(m+1,vector<int>(n+1,0));
        int mmx=0;
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(A[i-1]==B[j-1]){
                    ma[i][j]=ma[i-1][j-1]+1;
                }
                mmx=max(mmx,ma[i][j]);
            }
        }
        return mmx;
    }
};



Longest Increasing Continuous subsequence

Give you an integer array (index from 0 to n-1, where n is the size of this array),find the longest increasing continuous subsequence in this array. (The definition of the longest increasing continuous subsequence here can be from right to left or from left to right)

Example

For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.

For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.


Longest Palindromic Substring
Given a string  S , find the longest palindromic substring in  S . You may assume that the maximum length of  S  is 1000, and there exists one unique longest palindromic substring.


Longest Increasing Continuous subsequence II
Give you an integer matrix (with row size n, column size m),find the longest increasing continuous subsequence in this matrix. (The definition of the longest increasing continuous subsequence here can start at any row or column and go up/down/right/left any direction).

Example

Given a matrix:

[
  [1 ,2 ,3 ,4 ,5],
  [16,17,24,23,6],
  [15,18,25,22,7],
  [14,19,20,21,8],
  [13,12,11,10,9]
]

return 25






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值