当前位置:网站首页>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 alexnet
- LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
- 03 fastjason solves circular references
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- Octave instructions
- Toolbutton property settings
- Leetcode-513:找树的左下角值
- Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
- 01 business structure of imitation station B project
- LeetCode - 673. 最长递增子序列的个数
猜你喜欢
Leetcode 300 longest ascending subsequence
LeetCode - 919. Full binary tree inserter (array)
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Connect Alibaba cloud servers in the form of key pairs
LeetCode - 715. Range 模块(TreeSet) *****
Flutter 退出当前操作二次确认怎么做才更优雅?
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
Cases of OpenCV image enhancement
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Pymssql controls SQL for Chinese queries
随机推荐
Positive and negative sample division and architecture understanding in image classification and target detection
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
4G module designed by charging pile obtains signal strength and quality
RESNET code details
Discrete-event system
Leetcode - 933 number of recent requests
Swing transformer details-1
Leetcode-513: find the lower left corner value of the tree
Retinaface: single stage dense face localization in the wild
Leetcode-112:路径总和
Opencv Harris corner detection
Pymssql controls SQL for Chinese queries
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
openCV+dlib实现给蒙娜丽莎换脸
Opencv histogram equalization
Opencv feature extraction sift
[C question set] of Ⅵ
Vgg16 migration learning source code
4G module initialization of charge point design
QT setting suspension button