当前位置:网站首页>Leetcode day 17
Leetcode day 17
2022-07-04 12:35:00 【worldinme】
516. The longest palindrome subsequence
Give you a string s, find s The longest palindrome substring in .
Example 1:
Input :s = "babad"
Output :"bab"
explain :"aba" It's the same answer .
Example 2:
Input :s = "cbbd"
Output :"bb"
Tips :
1 <= s.length <= 1000
s It consists only of numbers and English letters
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/longest-palindromic-substring
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Realize the idea :
This problem has been done before , But this time, there are still many problems , Special attention should be paid here to the fact that the order of the two loops of the function body cannot be exchanged , First deal with a certain kind of palindrome string with fixed length , Handle in the extended drive .
Implementation code :
class Solution {
public String longestPalindrome(String s) {
int len=s.length();
if(len<2) return s;
int begin=0,end=0;
int maxlen=0;
boolean[][] dp=new boolean[len][len];
char[] charArray=s.toCharArray();
for(int i=0;i<len;i++){
dp[i][i]=true;
}
for(int l=2;l<=len;l++){
for(int i=0;i<len;i++){
int j=i+l-1;
if(j>=len){
break;
}
if(charArray[i]!=charArray[j]){
dp[i][j]=false;
}else{
if(l<=3){
dp[i][j]=true;
}else{
dp[i][j]=dp[i+1][j-1];
}
}
if(dp[i][j]==true&&l>maxlen){
maxlen=l;
begin=i;
end=j;
}
}
}
return s.substring(begin,end+1);
}
}
516. The longest palindrome subsequence
Realize the idea :
I have trouble writing the code of this problem , In fact, it is a variant of the previous question , Clarify the state transition equation .
I'm tired of writing code , But I still have the courage to post it .
Implementation code :
class Solution {
public int longestPalindromeSubseq(String s) {
int len=s.length();
if(len<2) return len;
int begin=0,end=0;
int maxlen=0;
int[][] dp=new int[len][len];
char[] charArray=s.toCharArray();
for(int i=0;i<len;i++){
dp[i][i]=1;
}
for(int l=2;l<=len;l++){
for(int i=0;i<len;i++){
int j=i+l-1;
if(j>=len){
break;
}
if(charArray[i]!=charArray[j]){
dp[i][j]=Math.max(dp[i+1][j],dp[i][j-1]);
}else{
if(l<=3){
dp[i][j]=l;
}else{
dp[i][j]=dp[i+1][j-1]+2;
}
}
}
}
return dp[0][len-1];
}
}
边栏推荐
- BCD code Baidu Encyclopedia
- Fastlane 一键打包/发布APP - 使用记录及踩坑
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
- How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
- netstat
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
- SAP ui5 date type sap ui. model. type. Analysis of the display format of date
- 'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
- LxC shared directory permission configuration
猜你喜欢
Some summaries of the 21st postgraduate entrance examination 823 of network security major of Shanghai Jiaotong University and ideas on how to prepare for the 22nd postgraduate entrance examination pr
[the way of programmer training] - 2 Perfect number calculation
Clion configuration of opencv
LVS load balancing cluster deployment - Dr direct routing mode
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
CSDN documentation specification
How to judge the advantages and disadvantages of low code products in the market?
Introduction to random and threadlocalrandom analysis
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
IPv6 experiment
随机推荐
Clion configuration of opencv
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
MySQL advanced (Advanced) SQL statement
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
nn. Exploration and experiment of batchnorm2d principle
Pat 1059 prime factors (25 points) prime table
Bottom Logic -- Mind Map
Snowflake won the 2021 annual database
Detailed explanation of classic process synchronization problems
Btrace tells you how to debug online without restarting the JVM
Ml and NLP are still developing rapidly in 2021. Deepmind scientists recently summarized 15 bright research directions in the past year. Come and see which direction is suitable for your new pit
The solution of permission denied
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
BCD code Baidu Encyclopedia
A few words explain redis cache penetration, breakdown, avalanche, and redis sentinel
Uva536 binary tree reconstruction tree recovery
22 API design practices
Azure solution: how can third-party tools call azure blob storage to store data?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23