当前位置:网站首页>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)
边栏推荐
- Small test (I)
- [noip2001 popularization group] the problem of maximum common divisor and minimum common multiple
- ES6 set and map
- Course notes of single chip microcomputer principle and interface technology for migrant workers majoring in electronic information engineering
- [noip2001 popularization group] packing problem
- The difference between the world wide web, the Internet and the Internet
- Golang log programming system
- [NOIP2001 普及组] 最大公约数和最小公倍数问题
- Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
- 2022-07-21 第四小组 修身课 学习笔记(every day)
猜你喜欢
随机推荐
ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
Remember SQL optimization once
STM——EXTI外部中断学习笔记
[sql] case expression
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
2022-07-21 group 4 polymorphism
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
ELS modify cursor, modify Icon
2022-07-21 study notes of group 4 self-cultivation class (every day)
ext4、ntfs、xfs、btrfs、zfs、f2fs和reiserFS性能对比
Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法
如何用U盘进行装机?
LoRa无线网关如何快速实现端到云的传输
[STL]优先级队列priority_queue
Type the URL to the web page display. What happened during this period?
Oxycon 2022 network capture frontier conference is about to open!
Is the galaxy VIP account opened by qiniu safe?
Unity quickly builds urban scenes
What's good for starting a business with 10000 yuan? Is we media OK?









![[NOIP2001 普及组]装箱问题](/img/b7/1310b3e68d0ee016465fc069315af6.png)