当前位置:网站首页>2022.05.27 (lc_647_palindrome substring)
2022.05.27 (lc_647_palindrome substring)
2022-06-10 19:43:00 【Leeli9316】

Method 1 : Violent solution (O(n^3))
class Solution {
public int countSubstrings(String s) {
int n = s.length();
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
if (satisfy(s.substring(i, j))) {
ans++;
}
}
}
return ans;
}
public boolean satisfy(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) return false;
i++;
j--;
}
return true;
}
}Method 2 : Dynamic programming (O(n^2))
① Determine the state of :dp[i][j] Express [i, j] Whether the string of is a palindrome substring ;
② Transfer equation :if (s.charAt(i) == s.charAt(j) && (j - i <= 2 || dp[i + 1][j - 1])) {
dp[i][j] = true;
};
③ Initial conditions and boundary conditions :dp[i][j] = false;
④ Computation order : because dp[i][j] from dp[i + 1][j - 1] Introduction , So the outer layer for Cyclic reverse order , Inner layer for Cyclic positive order .
class Solution {
public int countSubstrings(String s) {
int n = s.length();
//dp[i][j] Express [i,j] Whether the string of is a palindrome substring
boolean[][] dp = new boolean[n][n];
int ans = 0;
// Because of the demand dp[i][j] Need to know dp[i + 1][j - 1]
// So the outer loop needs to be written backwards (i--), Inner loop needs to be written (j++)
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
// When s.charAt(i) != s.charAt(j) when ,dp[i][j] It must be false
// When s.charAt(i) == s.charAt(j) And the number of elements is 1,2,3 or dp[i + 1][j - 1] by true when ,dp[i][j] It must be true
if (s.charAt(i) == s.charAt(j) && (j - i <= 2 || dp[i + 1][j - 1])) {
dp[i][j] = true;
ans++;
}
}
}
return ans;
}
}Method 3 : Center expansion (O(n^2))
class Solution {
public int countSubstrings(String s) {
int n = s.length();
int ans = 0;
for (int i = 0; i < n; i++) {
//j = 0, The palindrome center is a character
//j = 1, The palindrome center is two characters
for (int j = 0; j <= 1; j++) {
int left = i;
int right = i + j;
while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
left--;
right++;
ans++;
}
}
}
return ans;
}
}边栏推荐
- Tencent cloud database tdsql- a big guy talks about the past, present and future of basic software
- This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
- What are the current mainstream all-optical technology solutions- Part II
- Yuntu says that every successful business system cannot be separated from apig
- mysql(17-课后练习题)
- [advanced C language] data storage [part I] [ten thousand words summary]
- 个人如何投资理财比较安全?
- [C language] accidentally write a bug? Mortals teach you how to write good code [explain debugging skills in vs]
- Design and implementation of SSM based traffic metering cloud system Rar (thesis + project source code)
- 恭喜 | 医学院那洁课题组通过多组学分析揭示JUNB在体外分化人造血祖细胞过程中的功能
猜你喜欢
![[Agency] 10 minutes to master the essential difference between forward agency and reverse agency](/img/67/5f30f36aa60cf605cbc32399a9d9a0.png)
[Agency] 10 minutes to master the essential difference between forward agency and reverse agency

618大促将至,用AI挖掘差评,零代码实现亿级评论观点情感分析

HW蓝队中级面试复盘

Morris traversal of binary tree

MySQL高级篇第一章(linux下安装MySQL)【上】

云图说|每个成功的业务系统都离不开APIG的保驾护航

mysql(17-触发器)
![[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure](/img/94/c9c7935aa0c98eb39a34377ad02b10.png)
[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure

【web】個人主頁web大作業「課錶」「相册」「留言板」

Design and development of hospital reservation registration platform based on JSP Zip (thesis + project source code)
随机推荐
Prospect of database firewall technology [final chapter]
多通道信号数据压缩存储
Mysql database design concept (multi table query & transaction operation)
c(指针-02)
个人如何投资理财比较安全?
Chapter 6 relational data theory exercise
【C语言进阶】数据的存储【下篇】【万字总结】
frp reverse proxy
恭喜 | 医学院那洁课题组通过多组学分析揭示JUNB在体外分化人造血祖细胞过程中的功能
SAR图像聚焦质量评价插件
MySQL (17 after class exercises)
Debugging skills
MySQL (17 trigger)
腾讯Libco协程开源库 源码分析(三)---- 探索协程切换流程 汇编寄存器保存 高效保存协程环境
Go language learning notes - cross domain configuration, global exception capture | web framework gin (IV)
Domain Driven Design (VI) - Architecture Design
写作技术文章是留给自己未来的财富
Source code analysis and practical testing openfeign load balancing
[advanced C language] advanced pointer [Part 2]
腾讯Libco协程开源库 源码分析(二)---- 柿子先从软的捏 入手示例代码 正式开始探究源码