当前位置:网站首页>Leetcode - 5 longest palindrome substring
Leetcode - 5 longest palindrome substring
2022-07-03 10:12:00 【Cute at the age of three @d】

Dynamic programming

class Solution {
public String longestPalindrome(String s) {
int n = s.length();
boolean[][] dp = new boolean[n][n];
int len = 0;
String ans = "";
for(int i = n-1; i >=0;i--){
for(int j = i; j < n;j++){
if(j == i){
dp[i][j] = true;
}
else if(j - i == 1){
dp[i][j] = s.charAt(i) == s.charAt(j);
}
else{
dp[i][j] = s.charAt(i) == s.charAt(j) && dp[i+1][j-1];
}
if(dp[i][j] == true){
if(j-i+1 > len){
len = j-i+1;
ans = s.substring(i,j+1);
}
}
}
}
return ans;
}
}
Center diffusion

class Solution {
int len = 0;
String ans = "";
public String longestPalindrome(String s) {
int n = s.length();
for(int i = 0; i<n;i++){
centerSpread(s,i,i);
if(i < n-1)
centerSpread(s,i,i+1);
}
return ans;
}
public void centerSpread(String s,int left,int right){
int n = s.length();
int i = left;
int j = right;
while(i>=0 && j<n && s.charAt(i) == s.charAt(j))
{
i--;
j++;
}
if(j-i-1 > len){
len = j-i-1;
ans = s.substring(i+1,j);
}
}
}
边栏推荐
- 使用密钥对的形式连接阿里云服务器
- CV learning notes convolutional neural network
- 20220604数学:x的平方根
- Google browser plug-in recommendation
- Leetcode-112:路径总和
- Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
- 2.1 Dynamic programming and case study: Jack‘s car rental
- 20220601数学:阶乘后的零
- 2021-10-27
- Retinaface: single stage dense face localization in the wild
猜你喜欢

CV learning notes - clustering

Discrete-event system

Retinaface: single stage dense face localization in the wild

Opencv note 21 frequency domain filtering

Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)

Adaptiveavgpool1d internal implementation

『快速入门electron』之实现窗口拖拽

CV learning notes convolutional neural network

openCV+dlib实现给蒙娜丽莎换脸

LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
随机推荐
Problems encountered when MySQL saves CSV files
Opencv+dlib to change the face of Mona Lisa
MySQL root user needs sudo login
20220606数学:分数到小数
QT self drawing button with bubbles
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Replace the files under the folder with sed
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:
CV learning notes - clustering
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
4.1 Temporal Differential of one step
Discrete-event system
RESNET code details
Opencv notes 20 PCA
Octave instructions
Crash工具基本使用及实战分享
Toolbutton property settings
LeetCode - 919. 完全二叉树插入器 (数组)
[C question set] of Ⅵ