647. 回文子串

题目描述

  就是数子串中有多少是回文串

解法

  迭代,一开始我是自底向上:也就是按字符串长度1,2,3,…,len,如果字符串是回文串,就横向扩展,也就是说如果a[left-1] == a[right+1],就知道相邻的子串也是回文串。最终这种方法超时了。然后换成自顶向下:也就是从len,len-1,len-2,…,1,如果一个子串是回文串,那么这个子串的多个子串也都是回文子串。

class Solution {
    public int countSubstrings(String s) {
        int len = s.length();
        boolean[][] mark = new boolean[len][len];
        int res = 0;
        // for (int l = 1; l <= len; l++) {
        //     for (int i = 0; i <= len - l; i++) {
        //         int j = i + l - 1;
        //         if (mark[i][j]) {
        //             res++;
        //             continue;
        //         }
        //         if (i == j) {
        //             res++;
        //             mark[i][j] = true;
        //             continue;
        //         }
        //         if (isPali(s.substring(i, j+1))) {
        //             mark[i][j] = true;
        //             res++;
        //             int tmpi = i, tmpj = j;
        //             while (tmpi - 1 >= 0 && tmpj + 1 < len) {
        //                 if (s.substring(tmpi) == s.substring(tmpj)) {
        //                     mark[tmpi][tmpj] = true;
        //                 }
        //                 tmpi--;
        //                 tmpj++;
        //             }
        //         }
        //     }
        // }

        for (int l = len; l >= 1; l--) {
            for (int i = 0; i <= len - l; i++) {
                int j = i + l - 1;
                if (mark[i][j]) {
                    res++;
                    continue;
                }
                if (i == j) {
                    res++;
                    mark[i][j] = true;
                    continue;
                }
                if (isPali(s.substring(i, j+1))) {
                    mark[i][j] = true;
                    res++;
                    int left = i + 1, right = j - 1;
                    while (left <= right) {
                        mark[left][right] = true;
                        left++;
                        right--;
                    }
                }
            }
        }
        return res;
    }

    boolean isPali(String str) {
        int len = str.length();
        int left = 0, right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

题解

  题解方法关注的是回文串所有可能的回文串中心点,然后从中心点往两边扩散,比我这种要好很多。我这种其实是O(n3)。
  然后还有就是一个对我来说的新算法Manacher 算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值