当前位置:网站首页>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;
}
}边栏推荐
- 第二章 数据类型(一)
- How can bi help enterprises reduce labor, time and management costs?
- 端午“沉浸式云旅游”怎么玩?即构助力“直播+”新场景落地
- Use of uiautomator2 automated test tool
- 基于谱加权的波束方向图分析
- How to set up salesmartly for Google Analytics tracking
- 阵列信号处理仿真之四——Z变换分析阵列多项式
- Metadata management, the basic construction of enterprises in the digital era
- 如何设置 SaleSmartly 以进行 Google Analytics(分析)跟踪
- Chapter II data type (I)
猜你喜欢

【Vulnhub靶场】JANGOW: 1.0.1

Openssl1.1.1 compilation error can't locate win32/console pm in @INC

数据治理经典6大痛点?这本书教你解决

Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)

Request header field XXXX is not allowed by access control allow headers in preflight response

Ruijie x32pro brush openwrt enable wireless 160MHz

《Single Image Haze Removal Using Dark Channel Prior》去雾代码实现分析

The value of Bi in the enterprise: business analysis and development decision

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

VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
随机推荐
[kuangbin] topic 22 interval DP
VIM common shortcut keys
跨域报错:When allowCredentials is true, allowedOrigins cannot contain the special value “*“
Introduction to ad18 device library import
WordPress 6.0 “Arturo阿图罗” 发布
Adobe Premiere Foundation (the last step of video subtitle adding) (6)
5. golang generics and reflection
Ruijie x32pro brush openwrt enable wireless 160MHz
VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
直播预告 | 社交新纪元,共探元宇宙社交新体验
Detailed explanation of Lora module wireless transceiver communication technology
AEC:回声产生原因及回声消除原理解析
How to set up salesmartly for Google Analytics tracking
Libcurl 7.61.0 vs2013 compilation tutorial
The value of Bi in the enterprise: business analysis and development decision
Adobe Premiere Basic - tool use (select tools, rasoir tools, and other Common Tools) (III)
[database language SPL] a simple and fast database language SPL
Openssl1.1.1 compilation error can't locate win32/console pm in @INC
Db2 SQL PL中的控制语句
Stream生成的3张方式-Lambda