LeetCode 5:Longest Palindromic Substring(最长回文串)
2015-06-11 22:54
387 查看
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.
思路一:(超时)简单的截取长度为length(length递减)的字串,判断其是否为回文串。第一个满足要求的回文串最长。
View Code
思路三:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html这个方法效率最高,现在的水平还想不出这样的方法。
思路一:(超时)简单的截取长度为length(length递减)的字串,判断其是否为回文串。第一个满足要求的回文串最长。
public class Solution { public String longestPalindrome(String s) { int n = s.length(); int start = 0; int maxLength = 1; boolean dp[][] = new boolean[n+1][n+1]; for (int i = 0; i < n; i++) { dp[i][i] = true; } for (int i = 0; i < n-1; i++) { if (s.charAt(i) == s.charAt(i+1)) { dp[i][i+1] = true; start = i; maxLength = 2; } } for (int len = 3; len <= n; len++) { for (int i = 0; i < n-len+1; i++) { int j = i+len-1; if (s.charAt(i) == s.charAt(j)&& dp[i+1][j-1]) { dp[i][j] = true; start = i; maxLength = len; } } } return s.substring(start, start+maxLength); } }
View Code
思路三:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html这个方法效率最高,现在的水平还想不出这样的方法。
相关文章推荐
- 不是书评 :《我是一只IT小小鸟》
- hdu 4545 贪心 *
- 在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token
- java笔记05 函数
- 获取地址栏参数
- 服务器构架的基础1
- VS项目中lib文件的使用
- vs2012 C#调用c++ dll 调试跟踪到代码内
- 数据结构C语言实现复习
- 使用Apache Phoenix 实现 SQL 操作HBase
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- LAMP环境搭建2-Apache2.4
- get和post的区别
- OpenCL矩阵转置
- 用键盘获取一个随机的字符串(gets的用法)
- 2015061105 - Mysql大小写敏感的问题
- 4000 黑马程序员_90_网络编程Socket
- Java编程概述
- 在SQuirrel中使用Phoenix操作HBase——创建表和视图
- 2015061104 - 2015编程语言排行榜