leetcode 1147. 段式回文(巨水 注意边界)

题目

leetcode 1147. 段式回文(巨水 注意边界)_第1张图片

class Solution {
public:
    int longestDecomposition(string s) {
        int n=s.size(),ans=0;
        if(n==1) return 1;
        int l=0,j=n-1;
        while(l<=j){
            int tmpl=l,tmpj=j;
            while(l<j&&s.substr(tmpl,l-tmpl+1)!=s.substr(j,tmpj-j+1)) ++l,--j;
            if(l<j) ans+=2;//配对成功
            else ans+=1;//配对未成功
            ++l,--j;
        }
        return ans;
    }
};

你可能感兴趣的:(leetcode,#,巨水)