当前位置:网站首页>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);
}
}
}
边栏推荐
- Retinaface: single stage dense face localization in the wild
- Leetcode-404:左叶子之和
- 3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
- The underlying principle of vector
- Deep Reinforcement learning with PyTorch
- 2.2 DP: Value Iteration & Gambler‘s Problem
- MySQL root user needs sudo login
- CV learning notes - deep learning
- Leetcode-100:相同的树
- Development of intelligent charging pile (I): overview of the overall design of the system
猜你喜欢

openCV+dlib實現給蒙娜麗莎換臉

CV learning notes - deep learning

Leetcode 300 最长上升子序列

Opencv Harris corner detection

Leetcode bit operation

Deep learning by Pytorch

CV learning notes convolutional neural network

Pymssql controls SQL for Chinese queries

2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)

2. Elment UI date selector formatting problem
随机推荐
2021-10-28
01 business structure of imitation station B project
Opencv gray histogram, histogram specification
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
Leetcode - 933 number of recent requests
Leetcode 300 longest ascending subsequence
Tensorflow built-in evaluation
QT self drawing button with bubbles
getopt_ Typical use of long function
3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
Label Semantic Aware Pre-training for Few-shot Text Classification
Yocto technology sharing phase IV: customize and add software package support
ADS simulation design of class AB RF power amplifier
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
01 business structure of imitation station B project
QT detection card reader analog keyboard input
My 4G smart charging pile gateway design and development related articles
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
(2) New methods in the interface