当前位置:网站首页>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];
}
}边栏推荐
- Unity performance optimization reading notes - explore performance issues -profiler (2.1)
- [the way of programmer training] - 2 Perfect number calculation
- Btrace tells you how to debug online without restarting the JVM
- 13、 C window form technology and basic controls (3)
- 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
- Source code analysis of the implementation mechanism of multisets in guava class library
- Googgle guava ImmutableCollections
- Classification and application of AI chips
- Talk about "in C language"
- Guava ImmutableSet. Builder source code analysis, shift original code, complement code, reverse code review
猜你喜欢

记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题

Complementary knowledge of auto encoder
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16](/img/c3/f3746b161012acc3751b2bd0b8f663.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16

When synchronized encounters this thing, there is a big hole, pay attention!

ASP. Net razor – introduction to VB loops and arrays

Data communication and network: ch13 Ethernet
![Entitas learning [3] multi context system](/img/f9/a3ce86ff2121dd1043305b7e834cc5.jpg)
Entitas learning [3] multi context system

MySQL advanced review

(August 9, 2021) example exercise of air quality index calculation (I)

How to realize the function of Sub Ledger of applet?
随机推荐
MPLS experiment
Btrace tells you how to debug online without restarting the JVM
How to create a new virtual machine
asp. Core is compatible with both JWT authentication and cookies authentication
Reptile learning 4 winter vacation series (3)
Haproxy cluster
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
Ultimate bug finding method - two points
2020 Summary - Magic year, magic me
Guava ImmutableSet. Builder source code analysis, shift original code, complement code, reverse code review
DVC use case (VI): Data Registry
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
Global and Chinese markets for environmental disinfection robots 2022-2028: Research Report on technology, participants, trends, market size and share
How do std:: function and function pointer assign values to each other
How to realize the function of Sub Ledger of applet?
[notes] in depth explanation of assets, resources and assetbundles
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
Tableau makes data summary after linking the database, and summary exceptions occasionally occur.
CSDN documentation specification
[Android reverse] function interception instance (③ refresh CPU cache | ④ process interception function | ⑤ return specific results)