博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
647. Palindromic Substrings 互文的子字符串
阅读量:4324 次
发布时间:2019-06-06

本文共 1787 字,大约阅读时间需要 5 分钟。

[抄题]:

Given a string, your task is to count how many palindromic substrings in this string.

The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

Example 1:

Input: "abc"Output: 3Explanation: Three palindromic strings: "a", "b", "c".

 

Example 2:

Input: "aaa"Output: 6Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道和dp有什么关系:判断互文还是要用helper函数,dp只是写出由内而外的扩展方程

[一句话思路]:

由于自身就算互文串,所以自身扩展或相邻位扩展

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 递推表达式的范围正常写就行 不用-1 :for (int i = 0; i < s.length(); i++)

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

递推表达式正常写就行,反正都由void类型的ispalindrome控制,一言不合就退出 

public void isPalindromic(int left, int right, String s) {        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {            count++;            left--;            right++;        }    }

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

class Solution {    int count = 0;        public int countSubstrings(String s) {        //cc        if (s == null || s.length() == 0) return 0;                //ini                //for loop        for (int i = 0; i < s.length(); i++) {            isPalindromic(i, i, s);            isPalindromic(i, i + 1, s);        }                return count;    }        public void isPalindromic(int left, int right, String s) {        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {            count++;            left--;            right++;        }    }}
View Code

 

转载于:https://www.cnblogs.com/immiao0319/p/9003299.html

你可能感兴趣的文章
MySQL学习——MySQL数据库概述与基础
查看>>
ES索引模板
查看>>
HDU2112 HDU Today 最短路+字符串哈希
查看>>
JPanel重绘
查看>>
图片放大器——wpf
查看>>
SCALA STEP BY STEP
查看>>
cocos2d-x学习笔记
查看>>
MySql中的变量定义
查看>>
Ruby数组的操作
查看>>
hdu1181暴搜
查看>>
解码字符串 Decode String
查看>>
json学习笔记
查看>>
工具:linux 性能监控工具-nmon
查看>>
fatal error C1853
查看>>
Ural 1001 - Reverse Root
查看>>
玩转webpack之webpack的entry output
查看>>
java 操作mongodb查询条件的常用设置
查看>>
黑马程序员_java基础笔记(02)...java语言基础组成
查看>>
对innodb 拷贝文件实现数据库的方式(转)
查看>>
python知识点 2014-07-09
查看>>