当前位置:网站首页>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;
}
}边栏推荐
- MySQL (17 after class exercises)
- Nature Biotechnol | 李家洋/余泓团队利用平铺删除策略打破性状连锁,突破水稻产量瓶颈
- 100003 words, take you to decrypt the system architecture under the double 11 and 618 e-commerce promotion scenarios
- How to query the database table storage corresponding to a field on the sapgui screen
- 騰訊Libco協程開源庫 源碼分析(二)---- 柿子先從軟的捏 入手示例代碼 正式開始探究源碼
- MySql的MyISAM引擎切换InnoDB时报错Row size too large (&gt; 8126)解决
- Cet article vous donne un aperçu de la tâche future de j.u.c, du cadre Fork / join et de la file d'attente de blocage
- mysql8.0(新特性小结)
- LeetCode_并查集_中等_399. 除法求值
- 大厂是怎么写数据分析报告的?
猜你喜欢

SAR回波信号基本模型与性质

HW蓝队中级面试复盘

mysql8.0(新特性小结)

掌握高性能计算前,我们先了解一下它的历史

【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off
![[advanced C language] advanced pointer [Part 1]](/img/a7/7a6f5286307d80b553c11582cf1827.png)
[advanced C language] advanced pointer [Part 1]
![[advanced C language] data storage [part I] [ten thousand words summary]](/img/d3/077189a235fd688d4504b7a62456e3.png)
[advanced C language] data storage [part I] [ten thousand words summary]

Basic improvement - tree DP supplement

2022最强版应届生软件测试面试攻略,助你直通大厂

云图说|每个成功的业务系统都离不开APIG的保驾护航
随机推荐
C knowledge exercise
Code solution of simplex method (including super detailed code notes and the whole flow chart)
MySQL (17 after class exercises)
Source code analysis and practical testing openfeign load balancing
DDD landing practice repeat record of theoretical training & Event storm
How is it safe for individuals to invest in financial management?
多通道信号数据压缩存储
【web】个人主页web大作业「课表」「相册」「留言板」
SPSS introductory notes
大厂测试员年薪30万到月薪8K,吐槽工资太低,反被网友群嘲?
Sliding window maximum value problem
恭喜 | 医学院那洁课题组通过多组学分析揭示JUNB在体外分化人造血祖细胞过程中的功能
[C language] accidentally write a bug? Mortals teach you how to write good code [explain debugging skills in vs]
Some questions often asked during the interview. Come and see how many correct answers you can get
数据库防火墙技术展望【终章】
Developers changing the world - Yao Guang teenagers playing Tetris
数据库防火墙闪亮登场(好文共赏)
Morris traversal of binary tree
Basic model and properties of SAR echo signal
一文帶你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue