[抄题]:

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: 3
Explanation: Three palindromic strings: "a", "b", "c".

Example 2:

Input: "aaa"
Output: 6
Explanation: 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++;
}
}
}

最新文章

  1. PHP 数据库连接工具类(MySQLI函数包装)
  2. java网络编程2
  3. outlook 2016 for windows 每次刷新发送接收邮件会弹出登陆界面
  4. JDK各个版本的新特性jdk1.5-jdk8
  5. jQuery 中 on 方法-----给未来元素添加事件
  6. POJ 2393 贪心 简单题
  7. No data in the view dba_hist_undostat (文档 ID 1558157.1)
  8. link cut tree 入门
  9. 函数page_get_space_id
  10. POJ_2104_Kth_(主席树)
  11. html 标签释义
  12. RTP InitializeComponent() 报错
  13. ajax面试汇总
  14. 2017年7月Web服务器调查报告
  15. WEB框架-Django框架学习(一)-基础层级关系
  16. 大数据入门第二十天——scala入门(二)scala基础01
  17. RabbitMQ-从基础到实战(3)— 消息的交换(上)
  18. Unity中Web.Config文件的配置与调用
  19. IOS 7 更改导航栏文字到白色
  20. Linux基本操作命令及作用

热门文章

  1. [LeetCode系列]N皇后问题递归解法 -- 位操作方式
  2. ffmpeg/ffplay 添加实时的时间水印 (转)
  3. 在CentOS上把MySQL从5.5升级到5.6(转)
  4. Docker安装ShowDoc
  5. Kafka之sync、async以及oneway
  6. Gradle: Can&#39;t load library: native-platform.dll
  7. 操作系统-百科: UNIX
  8. 第三章 Istio基本介绍
  9. Spring表单标签
  10. Linux: su sudo sudoer