68.Palindromic Substrings(回文字符串的个数)
Level:
Medium
题目描述:
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".
思路分析:
将字符串中每个字符看成回文串中间的数,然后向两边扩展,由于串的长可能为奇数也可能为偶数,所以都得考虑
代码:
public class Solution{
int res;
public int countSubstrings(String s){
if(s==null||s.length()==0)
return 0;
for(int i=0;i<s.length();i++){
help(i,i,s);
help(i,i+1,s);
}
return res;
}
public void help(int start,int end,String s){
while(start>=0&&end<s.length()&&s.charAt(start)==s.charAt(end)){
start--;
end++;
res++;
}
}
}
68.Palindromic Substrings(回文字符串的个数)的更多相关文章
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- [LeetCode] 647. Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode 5. Longest Palindromic Substring & 回文字符串
Longest Palindromic Substring 回文这种简单的问题,在C里面印象很深啊.希望能一次过. 写的时候才想到有两种情况: 454(奇数位) 4554(偶数位) 第1次提交 cla ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 转载-----Java Longest Palindromic Substring(最长回文字符串)
转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...
- Longest Palindromic Substring (最长回文字符串)——两种方法还没看,仍需认真看看
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- ORI-621龙芯3A处理器CPCI刀片计算机
ORI-621龙芯3A处理器CPCI刀片计算机 一.产品简介 ORI -621是一款基于龙芯3A国产CPU处理器的特种CPCI刀片计算机.该产品成功地实现了服务器NUMA架构在国产特种计算机中的应用, ...
- node npm vue.js 笔记
cnpm 下载包的速度更快一些. 地址:http://npm.taobao.org/ 安装cnpm: npm install -g cnpm --registry=https://registry.n ...
- python的list内存分配算法
前提:python为了提高效率会为list预先分配一定的内存空间供其使用,避免在每次append等操作都去申请内存,下面简单分析下list的内存分配算法,主要就是两段. 1.当没有元素时,newsiz ...
- 11JSP基础
1.Jsp基础 1.1 简介 Jsp,全称 Java Server Page java服务页面,能提供java服务的页面 jsp vs html html: 由html标签组成的,输出静态内容. js ...
- 读书笔记一、pandas之series
转自 # 直接传入一组数据 from pandas import Series, DataFrame obj = Series([4, 2, 3]) obj 0 4 1 2 2 3 dtype: in ...
- linux7 grub配置文件 linux6 grub配置文件
在 grub 的 kernel 配置后面,添加 acpi_pad.disable=1 重启机器之后,开机就不会自动加载 acpi_pad 模块 一:linux6 [root@node2 ~]# cat ...
- Java性能调优工具(Linux)
为了能准确获得程序的性能信息,需要使用各种辅助工具.以下主要介绍了Linux上关于Java的系统性能分析工具,掌握这些工具,对于性能瓶颈定位.系统故障排查都有帮助. 1.top命令 [root@loc ...
- alert(1) to win 16
- bzoj3991 [SDOI2015]寻宝游戏 树链的并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3991 题解 貌似这个东西叫做树链的并,以前貌似写过一个类似的用来动态维护虚树. 大概就是最终的 ...
- ECS 按量付费转包年包月支持按周啦
功能场景 不需要别的理由,就是省钱. 以 华北1 ecs.t5-c1m2.xlarge ( 4vCPU 8GB ) 为例:按量付费一周需要 131元,而按周付费只需要 68元. 如果您正在使用按量付费 ...