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 ...
随机推荐
- Support for the experimental syntax 'decorators-legacy' isn't currently enabled (7:1):
1.产生原因:项目不支持装饰器 2.解决方法: 2.1 执行 yarn 安装完整依赖: 2.2 如果依赖时yarn.lock变化了,并且项目有git目录,则将提示的文件提交到git仓库 ? Are y ...
- Linux shell中自动完成登录
在写shell脚本时,需要登录到不同的服务器上执行相关命令,在未建立信任之前如何批量操作. 1.ssh 首次登录服务器时会提示RSA key fingerprint输入yes/no,可以通过下面的方法 ...
- onupdate
数据的初始化显示刚开始写在onupdate中,文档类中的数据更新之后,希望通过调用UpdateAllViews(FALSE)来实现视图的更新,可以实现!后来觉得不妥,想把初始化显示写在ondraw中, ...
- jinfo 干涉java runtime的jvm参数
https://blog.csdn.net/bolg_hero/article/details/78156311 jinfo使用介绍可以用来查看正在运行的Java应用程序的扩展参数,甚至支持在运行时, ...
- 02.action--新增精灵知识点
import cocos from cocos.actions import * class HelloWorld(cocos.layer.ColorLayer): # ColorLayer子类化为具 ...
- [CH5E02] A Little Shop of Flowers
问题描述 You want to arrange the window of your flower shop in a most pleasant way. You have F bunches o ...
- 检查电脑链接的网络是否支持ipv6
测试方法一:在浏览器地址栏输入网址“http://test-ipv6.com/”,在页面会给出您的ipv6网络测试结果 测试方法二:在浏览器地址栏输入网址“http://ipv6.jmu.edu.cn ...
- 【HDOJ6687】Rikka with Stable Marriage(Trie树,贪心)
题意:给定两个长均为n的序列a和b,要求两两配对,a[i]和b[j]配对的值为a[i]^b[j],求配对后的值之和的最大值 n<=1e5,a[i],b[i]<=1e9 思路:和字典序最大的 ...
- execute、executeQuery和executeUpdate之间的区别 转
转:http://blog.csdn.net/colin_fantasy/article/details/3898070 execute.executeQuery和executeUpdate之间的区别 ...
- android7.0对于SharedPreferences设置模式的限制
错误信息: 03-28 10:16:12.701 830 932 E AndroidRuntime: FATAL EXCEPTION: Thread-903-28 10:16:12.701 ...