当前位置:网站首页>2022.05.27(LC_647_回文子串)
2022.05.27(LC_647_回文子串)
2022-06-10 18:16:00 【Leeli9316】

方法一:暴力求解(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;
}
}方法二:动态规划(O(n^2))
①确定状态:dp[i][j]表示[i, j]的字符串是否为回文子串;
②转移方程:if (s.charAt(i) == s.charAt(j) && (j - i <= 2 || dp[i + 1][j - 1])) {
dp[i][j] = true;
};
③初始条件和边界情况:dp[i][j] = false;
④计算顺序:因为dp[i][j] 由 dp[i + 1][j - 1] 推出,所以外层for循环倒序,内层for循环正序。
class Solution {
public int countSubstrings(String s) {
int n = s.length();
//dp[i][j]表示[i,j]的字符串是否为回文子串
boolean[][] dp = new boolean[n][n];
int ans = 0;
//因为求dp[i][j]需要知道dp[i + 1][j - 1]
//所以外层循环需要倒着写(i--),内层循环需要正着写(j++)
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
//当s.charAt(i) != s.charAt(j)时,dp[i][j]一定为false
//当s.charAt(i) == s.charAt(j)且元素个数为1,2,3或dp[i + 1][j - 1]为true时,dp[i][j]一定为true
if (s.charAt(i) == s.charAt(j) && (j - i <= 2 || dp[i + 1][j - 1])) {
dp[i][j] = true;
ans++;
}
}
}
return ans;
}
}方法三:中心拓展(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,回文中心是一个字符
//j = 1,回文中心是两个字符
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;
}
}边栏推荐
- Common methods of stream flow lambder
- Salesmartly | add a new channel slack to help you close the customer relationship
- [kuangbin]专题二十二 区间DP
- Linked List
- Cross domain error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
- The value of Business Intelligence BI. Is visual report equal to Business Intelligence BI?
- openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
- Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~
- Real time business intelligence Bi (II): reasonable ETL architecture design to realize quasi real time Business Intelligence BI
- Huawei cloud hcde Cloud Road phase II: how does Huawei cloud help small and medium-sized manufacturing enterprises' digital transformation?
猜你喜欢

基于谱加权的波束方向图分析

Adobe Premiere foundation - opacity (matte) (11)

第一章 SQL操作符

Openssl1.1.1 VS2013-编译教程

Adobe Premiere基础-不透明度(蒙版)(十一)

TestNG的HelloWorld例子以及如何在命令行下运行

In the digital era, how can enterprises manage data security and ensure the security of data assets

Salesmartly | add a new channel slack to help you close the customer relationship

Adobe Premiere基础(视频的最后一步字幕添加)(六)

阵列信号处理仿真之四——Z变换分析阵列多项式
随机推荐
Opencv does not rely on any third-party database for face detection
关于YUV格式的一些总结
Array type of DB2 SQL pl
Adobe Premiere Foundation (animation production - Flexible animation) (VIII)
【杂谈】恭喜自己获得CSDN专家称号,努力终会换来结果
mysql(17-触发器)
MySQL高级篇第一章(linux下安装MySQL)【上】
第6章 关系数据理论练习
第161章 SQL函数 YEAR
SQL function
How to correctly understand the real-time nature of Bi?
Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~
"Digital transformation, data first", talk about how important data governance is to enterprises
3. Golang并发入门
Adobe Premiere foundation - time remapping (10)
第二章 数据类型(一)
RK1126 新添加一个模块
JS Standard
Huawei cloud hcde Cloud Road phase II: how does Huawei cloud help small and medium-sized manufacturing enterprises' digital transformation?
C知识练习