当前位置:网站首页>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 ransca & image similarity comparison hash
- Leetcode-513: find the lower left corner value of the tree
- 使用密钥对的形式连接阿里云服务器
- Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
- 20220603 Mathematics: pow (x, n)
- Opencv histogram equalization
- 20220606数学:分数到小数
- Markdown latex full quantifier and existential quantifier (for all, existential)
- 20220604数学:x的平方根
- Leetcode-106:根据中后序遍历序列构造二叉树
猜你喜欢

Connect Alibaba cloud servers in the form of key pairs

Leetcode bit operation

Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa

LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)

3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation

Opencv image rotation

Cases of OpenCV image enhancement

CV learning notes - feature extraction

LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)

LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
随机推荐
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
Dictionary tree prefix tree trie
openCV+dlib實現給蒙娜麗莎換臉
Crash工具基本使用及实战分享
LeetCode - 919. Full binary tree inserter (array)
Tensorflow2.0 save model
2. Elment UI date selector formatting problem
CV learning notes - feature extraction
El table X-axis direction (horizontal) scroll bar slides to the right by default
QT is a method of batch modifying the style of a certain type of control after naming the control
CV learning notes - deep learning
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Yocto Technology Sharing Phase 4: Custom add package support
4G module board level control interface designed by charging pile
Opencv feature extraction - hog
20220602数学:Excel表列序号
2.1 Dynamic programming and case study: Jack‘s car rental
Opencv notes 17 template matching
【C 题集】of Ⅵ
Leetcode-112: path sum