当前位置:网站首页>Illustration leetcode - 5. Longest palindrome substring (difficulty: medium)
Illustration leetcode - 5. Longest palindrome substring (difficulty: medium)
2022-07-26 03:16:00 【Javanese Muse】
One 、 subject
Give you a string s, find s The longest palindrome substring in .
Two 、 Example
2.1> Example 1:
【 Input 】s = "babad"
【 Output 】"bab"
【 explain 】"aba" It's the same answer .
2.2> Example 2:
【 Input 】s = "cbbd"
【 Output 】"bb"
Tips :
1 <= s.length <= 1000sIt consists only of numbers and English letters
3、 ... and 、 Their thinking
3.1> Diverge to both sides through the center point
Because this question is to find the longest palindrome string , therefore , We can take measures after determining the center point , The way to expand to both sides , To judge “ scanning ” Is the substring to palindrome , If it is , Then continue to both sides “ Expand ”, Until the palindrome condition is not met . Suppose we find a palindrome string , There are two cases of the number of characters , namely :“ Odd number ” Number of characters and “ even numbers ” The number of characters .
about Number of odd characters , The center point is a character ( namely :head == tail), Then the palindrome will be written in 2*N + 1 Extend the length of . As shown in the figure below :

about Number of even characters , The center point is two characters ( namely :head + 1 == tail), Then the palindrome will be written in 2*N + 2 Extend the length of . As shown in the figure below :

Overall traversal from the beginning character to the end character , In the process of traversing , When it is found that the assembled palindrome character is the longest at this stage , Record it mid and distinct( Palindrome character length ), When all characters are traversed , adopt String Of substring Method to take a screenshot of a substring .
Four 、 Code implementation
4.1> Realization : Diverge to both sides through the center point
class Solution {
public String longestPalindrome(String s) {
if (s == "" || s == null) {
return null;
}
int mid = 0, distinct = 0;
for (int i = 0; i < s.length(); i++) {
int temp = Math.max(search(s, i, i), search(s, i, i + 1));
mid = distinct < temp ? i : mid;
distinct = Math.max(distinct, temp);
}
int num = (distinct % 2 == 0) ? 1 : 0;
return distinct == 0 ? null : s.substring(mid - distinct/2 + num, mid + distinct/2 + 1);
}
/**
* Return palindrome character length
* If the return is 【 even numbers 】, shows " Center point " There are two elements , namely :head + 1 == tail
* If the return is 【 Odd number 】, shows " Center point " Is an element , namely :head == tail
*/
public int search(String s, int head, int tail) {
while (head >= 0 && tail < s.length() && s.charAt(head) == s.charAt(tail)) {
head--;
tail++;
}
return tail - head - 1;
}
}
That's all for today's article :
Writing is not easy to , An article completed by the author in hours or even days , I only want to exchange you for a few seconds give the thumbs-up & Share .
More technical dry goods , Welcome everyone to follow the official account @ Java Muse 「 Dry cargo sharing , Update daily 」
Title source : Power button (LeetCode)
边栏推荐
- QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
- Unity快速搭建城市场景
- The difference between the world wide web, the Internet and the Internet
- Managing databases in a hybrid cloud: eight key considerations
- Use Anaconda to configure the tensorflow of GPU Version (less than 30 series graphics cards)
- 经典面试问题——OOP语言的三大特征
- Opencv报错:(parameter or structure field))Unrecognized or unsupported array type in functon ‘cvGetMat‘
- hello world驱动(二)-初级版
- Implement a method to find the sum of the number k and m in the array
- ELS modify cursor, modify Icon
猜你喜欢

Opencv 以指定格式保存图片

【无标题】

Opencv annotates the image (picture frame + writing)

VR panoramic shooting and production of business center helps businesses effectively attract people

Swin Transformer【Backbone】

The difference between the world wide web, the Internet and the Internet

离线数据仓库从0到1-阶段二软件安装

称霸薪酬榜!什么行业大有“钱”途?

canvas——绘制曲线——挂钟,饼图,五角星

There are a group of students in the class who have got the test results in Chinese and mathematics. Please select the students whose total score is the first
随机推荐
Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection
Swin Transformer【Backbone】
2022-07-21 第四小组 修身课 学习笔记(every day)
YOLOv3: An Incremental Improvement
Canvas - drawing pictures - dynamic drawing production
Summary of Huawei virtualization fusioncompute knowledge points
Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
YOLOv3: An Incremental Improvement
Type the URL to the web page display. What happened during this period?
How to install with USB flash disk?
图解LeetCode——5. 最长回文子串(难度:中等)
78. 子集
QT notes - Q_ Q and Q_ D learning
Use eventlog analyzer for log forensics analysis
小测(一)
【TensorFlow&PyTorch】图像数据增强API
线性回归原理推导
离线数据仓库从0到1-阶段一资源购买配置
【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
GoLang 抽奖系统 设计