当前位置:网站首页>【leetcode】647.回文子串
【leetcode】647.回文子串
2022-04-21 10:54:00 【前端corner】
题目
给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目。
回文字符串 是正着读和倒过来读一样的字符串。
子字符串 是字符串中的由连续字符组成的一个序列。
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
示例 1:
输入:s = “abc”
输出:3
解释:三个回文子串: “a”, “b”, “c”
示例 2:
输入:s = “aaa”
输出:6
解释:6个回文子串: “a”, “a”, “a”, “aa”, “aa”, “aaa”
提示:
1 <= s.length <= 1000
s 由小写英文字母组成
思路

思路一:双重for循环暴力
思路二:动态规划
- 设
dp[i][j]表示区间[i,j](左闭右闭)的子串是否是回文串,是则为true,否则为false - 递推关系,分为2种情况
- 情况一:
s[i] !== s[j],那么区间[i,j]的子串一定不是回文串,dp[i][j] = false - 情况二:
s[i] === s[j],此时又要分为3种情况考虑- 当
i === j,比如'a'这种是回文串,dp[i][j] = true - 当
j - i === 1,比如aa这种也属于回文串,dp[i][j] = true - 当
j - i > 1,那么就要看[i+1 ,j-1]是否为回文串,当dp[i+1][j-1] === true时,dp[i][j] = true;当dp[i+1][j-1] === false时,dp[i][j] = false
- 当
- 情况一:
- dp数组全部初始化为false
- 由情况二中的第三种情况,需要根据
dp[i+1][j-1]的值来得到dp[i][j]的值,而dp[i+1][j-1]在dp[i][j]的左下角,故遍历时从左到右,从下到上。
代码

思路一:暴力
/** * @param {string} s * @return {number} */
var countSubstrings = function(s) {
function isPalindrome(s , start , end){
let str = s.slice(start , end + 1)
return str === str.split('').reverse().join('')
}
let ans = 0
for(let i = 0 ; i < s.length ; i++){
for(let j = i ; j < s.length ; j++){
if(isPalindrome(s , i , j)) ans++
}
}
return ans
};
思路二:动态规划
/** * @param {string} s * @return {number} */
var countSubstrings = function(s) {
let len = s.length
let ans = 0
let dp = (new Array(len)).fill(false).map(x => (new Array(len)).fill(false))
for(let i = len - 1 ; i >= 0 ; i--){
for(let j = i ; j < len ; j++){
if(s[i] === s[j]){
if(j - i <= 1 || dp[i+1][j-1]){
dp[i][j] = true
ans++
}else{
dp[i][j] = false
}
}
}
}
return ans
};
复杂度
-
时间复杂度
- 思路一: O ( n 3 ) O(n^3) O(n3)
- 思路二: O ( n 2 ) O(n^2) O(n2)
-
空间复杂度
- 思路一: O ( n ) O(n) O(n)
- 思路二: O ( n 2 ) O(n^2) O(n2)
关注我的专栏,每天更新三道leetcode题解,一起变强!
版权声明
本文为[前端corner]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laplacepoisson/article/details/124315424
边栏推荐
- 刷题&比赛&复习
- 【天梯赛】L3-005 垃圾箱分布(堆优化版dijkstra)
- 2022 information and future preparation topic 2 new online judge 1113: digit problem
- The PHP file contains: require, require_ once; include,include_ once
- Local IP addresses are accessed using domain names
- 手把手教你:基于深度学习的滚动轴承故障诊断
- 二分查找符合要求的值及局部最小值
- GO 使用channel进行同步 (channel 1)
- Tamigou enterprise M & a platform, list of legal service providers!
- Convbert: improving Bert with span based dynamic revolution
猜你喜欢

Vulnhub PRIME: 1

Activity registration | how to quickly complete the development of data sources and targets based on the open source project tapdata PDK?

AcWing 1737. Transmission (classified discussion)

2022 information and future preparation topic 4 new online judge 2034: pruning shrubs

ConvBERT: Improving BERT with Span-based Dynamic Convolution论文的阅读笔记

How to write product requirements document (PRD) with the idea of five elements of user experience

活动报名 | 如何基于开源项目 Tapdata PDK,快速完成数据源和目标的开发?

IDEA和PyCharm启动时进入欢迎界面

8-channel can FD, more powerful data recorder gl3400

torch. autograd. Function customization
随机推荐
伦敦金在哪里开户安全?
AcWing 1725. Team tic tac toe game (enumeration)
Runhe Dayu learning program
MySQL modifies the maximum number of connections
Showcase时手机不够怎么办? 云真机平台atxserver2
AcWing 1737. Transmission (classified discussion)
The PHP file contains: require, require_ once; include,include_ once
Applet lifecycle
00000000000000000000000
torch. autograd. Function customization
Tgpimage in GDI + loads images from streams
塔米狗企业并购平台上,法律服务商名单目录!
AcWing 1725. 组队井字游戏(枚举)
RMQ&线段树复习
Go语言错误处理
【acwing】1459. Cow Gymnastics (simulation, thinking)
postman设置环境变量,简单又实用
Enter the welcome interface when idea and pycharm are started
OpenShift 4 - 提升客户端访问 API Server 安全
[wcn685x] how to determine which bdwlan file is called by WiFi driver?