当前位置:网站首页>LeetCode - 5 最长回文子串
LeetCode - 5 最长回文子串
2022-07-03 09:20:00 【三岁就很萌@D】

动态规划

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;
}
}
中心扩散

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);
}
}
}
边栏推荐
- There is no specific definition of embedded system
- 4G module initialization of charge point design
- 03 fastjason solves circular references
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- 2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
- LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
- Liquid crystal display
- Exception handling of arm
- Problems encountered when MySQL saves CSV files
- 51 MCU tmod and timer configuration
猜你喜欢

Basic knowledge of communication interface

Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn

LeetCode - 673. Number of longest increasing subsequences

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way

Opencv note 21 frequency domain filtering

Vgg16 migration learning source code

Leetcode 300 longest ascending subsequence

LeetCode - 673. 最长递增子序列的个数

I think all friends should know that the basic law of learning is: from easy to difficult

yocto 技術分享第四期:自定義增加軟件包支持
随机推荐
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Dictionary tree prefix tree trie
Serial port programming
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
Opencv note 21 frequency domain filtering
Yocto technology sharing phase IV: customize and add software package support
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
2.Elment Ui 日期选择器 格式化问题
Application of 51 single chip microcomputer timer
QT self drawing button with bubbles
LeetCode - 673. Number of longest increasing subsequences
03 fastjason solves circular references
4G module board level control interface designed by charging pile
自動裝箱與拆箱了解嗎?原理是什麼?
4G module designed by charging pile obtains signal strength and quality
LeetCode - 706 设计哈希映射(设计) *
My 4G smart charging pile gateway design and development related articles
03 fastjason solves circular references