当前位置:网站首页>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];
}
}
边栏推荐
- Wechat video Number launches "creator traffic package"
- Games101 Lesson 8 shading 2 Notes
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- 2021 annual summary - it seems that I have done everything except studying hard
- Global and Chinese markets of digital PCR and real-time PCR 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL advanced (Advanced) SQL statement
- 01. Basics - MySQL overview
- template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
猜你喜欢
Decrypt the advantages of low code and unlock efficient application development
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
Review of week 278 of leetcode II
Fastlane 一键打包/发布APP - 使用记录及踩坑
Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
Realize cross tenant Vnet connection through azure virtual Wan
MySQL advanced review
13、 C window form technology and basic controls (3)
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
随机推荐
AI should take code agriculture? Deepmind offers a programming version of "Alpha dog" alphacode that surpasses nearly half of programmers!
Global and Chinese markets of digital PCR and real-time PCR 2022-2028: Research Report on technology, participants, trends, market size and share
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
C语言:求100-999是7的倍数的回文数
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
Translation D29 (with AC code POJ 27:mode of sequence)
VBA, JSON interpretation, table structure -json string conversion
Decrypt the advantages of low code and unlock efficient application development
Review of week 278 of leetcode II
Leetcode: 408 sliding window median
Map container
Detailed explanation of classic process synchronization problems
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
netstat
[ES6] template string: `string`, a new symbol in es2015
When synchronized encounters this thing, there is a big hole, pay attention!
DDS-YYDS
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
C语言:求字符串的长度
【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法