zoukankan      html  css  js  c++  java
  • Leetcode 647. Palindromic Substrings

    Given a string s, return the number of palindromic substrings in it.

    A string is a palindrome when it reads the same backward as forward.

    A substring is a contiguous sequence of characters within the string.

    题目

    class Solution {
    public:
        int countSubstrings(string s) {
            n=s.size();
            int sum=0;
            for(int i=0;i<n;++i){
                sum+=count(s,i,i);
                sum+=count(s,i,i+1);
            }
            return sum;
        }
    private:
        int n;
        int count(string&s,int l,int r){
            int index=0;
            while(l>=0&&r<n&&s[l]==s[r]){
                --l;
                ++r;
                ++index;
            }
            return index;
        }
    };
    
  • 相关阅读:
    20210524
    20210521
    20210520
    20210519
    20210518
    20210517
    字符设备驱动三
    字符设备驱动二
    字符设备驱动一
    git基本操作
  • 原文地址:https://www.cnblogs.com/chanceYu/p/15238713.html
Copyright © 2011-2022 走看看