Longest Palindromic Substring - 字符串中最长的回文字段

简介: 需求:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string 判断回文数,中间开花。

需求:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string

判断回文数,中间开花。定一个中心,向两边散开。这个中心是从左到右移动的。需要2个游标。

int palindrome(String ps,int left,int right) 这个方法用来判断回文字段,返回字段长度

String longestPalindrome(String s) 在这里调用palindrome方法,遍历字符串去找。遍历过程注意不要越界。

以下为Java代码:

 1 /**
 2  * @author Rust Fisher
 3  * @date 2015-9-14
 4  */
 5 public class LongestPalindromicSubstring {
 6     /**
 7      * @param s - input string
 8      * @return the Longest Palindromic Substring 
 9      */
10     public static String longestPalindrome(String s) {
11         String result = "";
12         int inputLenght = s.length();
13         int startIndex = 0;
14         int longest = 1;
15         
16         for (int i = 0; i < inputLenght; i++) {
17             int oddLen = 1,dualLen = 1, currentLen;
18             oddLen = palindrome(s, i, i);
19             if (i+1 < inputLenght) {
20                 dualLen = palindrome(s, i, i+1);
21             }
22             currentLen = dualLen > oddLen ? dualLen : oddLen;
23             if (currentLen > longest){
24                 longest = currentLen;
25                 if (longest%2 == 0) {
26                     startIndex = i - longest/2 + 1;
27                 } else {
28                     startIndex = i - longest/2;
29                 }
30             }
31         }
32         for (int i = startIndex; i < startIndex+longest; i++) {
33             result += s.charAt(i);
34         }
35         return result;
36     }
37     /**
38      * @param ps - input string
39      * @param left - index move left
40      * @param right - index move right
41      * @return the current length of palindrome
42      */
43     public static int palindrome(String ps,int left,int right){
44         int thislen = 0;
45         int len = ps.length();
46         while(left >= 0 && right < len && ps.charAt(left) == ps.charAt(right)){
47             left--;
48             right++;
49         }
50         thislen = right - left - 1;
51         return thislen;
52     }
53     public static void main(String args[]){
54         System.out.println(longestPalindrome("hfiwafhaabbaaccddio128213"));
55         System.out.println(longestPalindrome("abcdefgfedcba"));
56         System.out.println(longestPalindrome("abc"));
57     }
58 }

输出:

aabbaa
abcdefgfedcba
a

目录
相关文章
EVE-NG的Windows客户端安装
EVE-NG提供Windows的客户端,集成了Wireshark、VNC、putty等软件,主要为完成配套EVE-NG的WEB浏览中的数据抓包等功能。 我的EVE-NG的Windows客户端安装包为EVE-NG-Win-Client-Pack.exe。
|
存储 弹性计算 调度
基于Knative的LLM推理场景弹性伸缩方案
Knative的基于请求弹性配置与大语言模型(LLM)的推理场景高度契合。此外,它的资源降配特性可以显著帮助用户降低成本。本文详细介绍基于 Knative 的 LLM 推理场景弹性伸缩方案。
|
网络协议 Java 测试技术
阿里内部Netty实战小册,值得拥有
Netty 是一个高性能的 Java 网络通信框架,简化了网络编程并涵盖了最新的Web技术。它提供了一种抽象,降低了底层复杂性,使得更多开发者能接触网络编程。Netty 因其易用性、高效性和广泛的应用场景受到推崇,适合互联网行业从业者学习,有助于理解和开发基于Netty的系统。免费的《Netty实战小册》详细介绍了Netty的各个方面,包括概念、架构、编解码器、网络协议和实际案例,帮助读者深入理解和应用Netty。如需完整版小册,可点击链接获取。
阿里内部Netty实战小册,值得拥有
|
druid Java
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
1212 0
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
|
消息中间件 Linux
RabbitMQ最大连接数
RabbitMQ最大连接数
1355 0
|
SQL 运维 监控
MySQL死锁系列-线上死锁问题排查思路
本篇文章会讲解一下如果线上发生了死锁异常,如何去排查和处理。除了系列前文讲解的有关加锁和锁冲突的原理还,还需要对 MySQl 死锁日志和 binlog 日志进行分析。
MySQL死锁系列-线上死锁问题排查思路
|
C# Windows 监控
WPF应用跨界成长秘籍:深度揭秘如何与Windows服务完美交互,扩展功能无界限!
【8月更文挑战第31天】WPF(Windows Presentation Foundation)是 .NET 框架下的图形界面技术,具有丰富的界面设计和灵活的客户端功能。在某些场景下,WPF 应用需与 Windows 服务交互以实现后台任务处理、系统监控等功能。本文探讨了两者交互的方法,并通过示例代码展示了如何扩展 WPF 应用的功能。首先介绍了 Windows 服务的基础知识,然后阐述了创建 Windows 服务、设计通信接口及 WPF 客户端调用服务的具体步骤。通过合理的交互设计,WPF 应用可获得更强的后台处理能力和系统级操作权限,提升应用的整体性能。
478 0
|
5G 网络架构
5G 系统网络架构 | 带你读《5G 无线系统设计与国际标准》之四
为了适应各种部署场景,5G 支持了两种部署方式:一种为分布式部署,这种方式与 LTE系统类似,网络由基站组成,基站支持全协议栈的功能;另一种为集中式部署,基站进一步分为集中单元(CU,Centralized Unit)和分布单元(DU,Distributed Unit)两个节点,CU 和 DU 分别支持不同的协议栈和功能,
5G 系统网络架构  | 带你读《5G 无线系统设计与国际标准》之四