当前位置:网站首页>Force deduction 5: Longest palindrome substring
Force deduction 5: Longest palindrome substring
2022-07-26 06:28:00 【weixin_ fifty-four million ninety-six thousand two hundred and 】
subject : Palindrome string ( Left to right and right to left strings are the same )

Ideas :
1. Suppose the length of the string is 1;
2. Set a two-dimensional array of boolean type , Using two Pointers i,j Traverse them separately i=0,j<=i;
3. Judge , Use charAt() Method to judge , Whether two strings are equal , And whether their length is equal to 2
Code :
class Solution {
public String longestPalindrome(String s) {
// Special case judgment
if(s.length() < 2 || s == null) return s;
int len = s.length(); // Length of string
int start = 0; // The starting point of the longest palindrome substring
int end = 0; // The end of the longest palindrome substring
int max = 1; // The length of the longest palindrome substring
boolean[][] dp = new boolean[len][len];// Create a 2D array , For storage status
for(int i = 0; i < len; i++){
for(int j = 0; j <= i; j++){
if(s.charAt(j) == s.charAt(i) && (i-j < 2 || dp[j+1][i-1])){
dp[j][i] = true;
if(i-j+1 > max){
start = j;
end = i;
max = Math.max(max, i-j+1);
}
}
}
}
return s.substring(start, end+1);
}
}
details :
charAt() Method to return the characters at the specified index . Index range is from 0 To length() - 1.
Reference resources :
边栏推荐
- C language explanation series - comprehensive exercises, guessing numbers games
- Workflow activiti5.13 learning notes (I)
- [day_020419] sort subsequence
- [pytorch] fine tuning technology
- 将一个正整数分解质因数,要求分解成尽可能小的多个的因数。
- 09 eth smart contract
- Go 的切片与数组
- 【Day03_0420】C语言选择题
- Vision Transformer 必读系列之图像分类综述
- IP day 10 notes - BGP
猜你喜欢

@ConstructorProperties注解理解以及其对应使用方式

Niuke network: TOPK problem of additive sum between two ordinal groups

分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

【无标题】

Cdga | how to build data asset catalogue?

RNN循环神经网络

带你搞透IO多路复用原理(select、poll和epoll)

Vision Transformer 必读系列之图像分类综述

Upgrade appium automation framework to the latest 2.0
随机推荐
C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
[day02_0419] C language multiple choice questions
【Day_05 0422】统计回文
【C语言】通讯录动态版和文件版
C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
[pytorch] CNN practice - flower species identification
Markdown add Emoji expression
2022年下半年系统集成项目管理工程师(软考中级)报名条件
Introduction of four redis cluster schemes + comparison of advantages and disadvantages
[day_060423] no two
Leetcode:934. The shortest Bridge
[day_020419] sort subsequence
【pytorch】图片增广
【Day_04 0421】进制转换
Interpretation of TPS motion (cvpr2022) video generation paper
输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
The number of weeks of Oracle last year and this year, with the start time and end time
Intelligent fire protection application based on fire GIS system
BPG notes (IV)
Convolutional neural network (III) - target detection