leetcode-ブラシ問題記録20200411-DP

1254 ワード

DP-最長回文サブシーケンス
タイトル:文字列sを指定し、sの中で最も長い回文サブ列を見つけます.sの最大長さは1000と仮定できます.
アルゴリズム解析:1暴力法:文字列に基づいて各文字列が両側に最も長い回文文字列pythonを見つける:
class Solution:
    def longestPalindrome(self, s: str) -> str:
        #   , i       ,         
        def lengthPalidrome(s,l,r):
            # i      
            while l>=0 and rlen(res):
                    res=lengthPalidrome(s,i,i+j)
        return res

C++
class Solution {
public:
    string longestPalindrome(string s) {
        string res="";
        int resLength=0;
        int tmp=0;
        if(s.length()==0){
            return res;
        }

        for(int i=0;iresLength){
                    res=sTmp;
                    resLength=tmp;
                }
            }
        }
        return res;
        
    }
    string lengthPalindrome(const string &s, int length, int l, int r, int *pLen){
        while(l>=0 && r