当前位置:网站首页>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];
}
}边栏推荐
- 22 API design practices
- Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography
- [solve the error of this pointing in the applet] SetData of undefined
- Kivy教程之 08 倒计时App实现timer调用(教程含源码)
- BCD code Baidu Encyclopedia
- Guava ImmutableSet. Builder source code analysis, shift original code, complement code, reverse code review
- Haproxy cluster
- The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
- Global and Chinese market of piston rod 2022-2028: Research Report on technology, participants, trends, market size and share
- (August 9, 2021) example exercise of air quality index calculation (I)
猜你喜欢

ASP. Net razor – introduction to VB loops and arrays

Decrypt the advantages of low code and unlock efficient application development

2021 annual summary - it seems that I have done everything except studying hard

Tableau makes data summary after linking the database, and summary exceptions occasionally occur.
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16](/img/c3/f3746b161012acc3751b2bd0b8f663.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17](/img/85/2635afeb2edeb0f308045edd1f3431.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17

Practical dry goods: deploy mini version message queue based on redis6.0

The detailed installation process of Ninja security penetration system (Ninjitsu OS V3). Both old and new VM versions can be installed through personal testing, with download sources

Memory computing integration: AI chip architecture in the post Moorish Era

Complementary knowledge of auto encoder
随机推荐
Single spa, Qiankun, Friday access practice
Lecture 9
How do std:: function and function pointer assign values to each other
Entity framework calls Max on null on records - Entity Framework calling Max on null on records
03_ Armv8 instruction set introduction load and store instructions
MPLS experiment
Snowflake won the 2021 annual database
(August 10, 2021) web crawler learning - Chinese University ranking directed crawler
Method of setting default items in C # ComboBox control code
OSI seven layer model & unit
Clion configuration of opencv
Entitas learning [3] multi context system
Complementary knowledge of auto encoder
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
Global and Chinese market of dental elevators 2022-2028: Research Report on technology, participants, trends, market size and share
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
[the way of programmer training] - 2 Perfect number calculation
Googgle guava ImmutableCollections
Global and Chinese markets for environmental disinfection robots 2022-2028: Research Report on technology, participants, trends, market size and share