C++ bottom-up solution


  • 0
    J

    bottom-up easy solution

    class Solution {
    public:
    int countSubstrings(string s) {
    int total = 0;
    for(int i = 0;i<s.size();i++)
    {
    int count1 = palindromic(s,i,i);
    int count2 = palindromic(s,i,i+1);//这里需要考虑两种情况下的扩展。
    total+=count1+count2;
    }
    return total;
    }

    int palindromic(string s,int start,int end)
    {
        int count = 0;
        while(start>=0&&end<s.size() && s[start]==s[end]) 
        {
            count++;
            start--;
            end++;
        }
        return count;
    }   
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.