https://leetcode.com/problems/palindromic-substrings/description/

https://www.cnblogs.com/grandyang/p/7404777.html

博客中写的<=2,实际上<=1也是可以的

相当于判断一个大指针内所有子字符串是否可能为回文

class Solution {
public:
    int countSubstrings(string s) {
        int length = s.size();
        int res = 0;
        vector<vector<bool>> dp(length+1,vector<bool>(length+1,false));
        for(int i = 1;i <= length;i++){
            for(int j = 1;j <= i;j++){
                if(s[i-1] == s[j-1]){
                    if(i -j <= 1 || dp[i-1][j+1]){
                        dp[i][j] = true;
                        res++;
                    }
                }                  
            }
        }
        return res;
    }
};

 

posted @ 2019-03-08 18:45  有梦就要去实现他  阅读(124)  评论(0编辑  收藏  举报