5.Longest Palindrome substring
/*
* 5.Longest Palindrome substring
* 2016-4-9 by Mingyang 自然而然的想到用dp来做
* 刚开始自己做的时候分的条件太细,两个index相等,相差小于3,还有其他
* 但这里这个if写的很好,一个代表了所有为true的情况
* 根本不用管为false的情况,因为自然而然为false
*
*/
public String longestPalindrome(String s) {
String res = " ";
if (s == null || s.length() == 0)
return "";
int len = s.length();
boolean[][] dp = new boolean[len][len];
int maxLen = 0;
for (int i = len - 1; i >= 0; i--) {
for (int j = i; j < len; j++) {
if (s.charAt(i) == s.charAt(j)
&& (j - i <= 2 || dp[i + 1][j - 1])) {
dp[i][j] = true;
if (maxLen < j - i + 1) {
maxLen = j - i + 1;
res = s.substring(i, j + 1);
// 这是用来切断一个string,使之只取一个小部分的值
}
}
}
}
return res;
}
5.Longest Palindrome substring的更多相关文章
- G.Longest Palindrome Substring
链接:https://ac.nowcoder.com/acm/contest/908/G 题意: A palindrome is a symmetrical string, that is, a st ...
- [LeetCode] Longest Palindrome Substring 具体分析
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【算法】最长回文子串 longest palindrome substring
对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. ...
- LeetCode the longest palindrome substring
回文检测,参考http://blog.csdn.net/feliciafay/article/details/16984031 使用时间复杂度和空间复杂度相对较低的动态规划法来检测,具体的做法 图一 ...
- 最长回文字串 (The longest palindrome substring)
这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
随机推荐
- grep-sed命令用法:
用户切换 su username:非登录式切换 su - username:登录式切换 su -l username:登录式切换 su username -c COMMAND echo -n ...
- Mysql的慢日志
一.开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二.慢日志参数: slow_query_log 慢查询开启状态slow_qu ...
- css3的border-radius属性使用方法
1.border-radius可以包含两个参数值,第一个水平圆角半径,第二个为垂直半径,并且两个参数值用“/”分开. 2.border-radius:设置一个值为四个角都相同,两个值为左上和右下相同, ...
- 时间格式的处理和数据填充和分页---laravel
时间格式文档地址:http://carbon.nesbot.com/docs/ 这是些时间格式,只需要我们这么做就可以 我们在模板层,找到对应的模型对象那里进行处理就可以啦 2018-11-08 16 ...
- 【curl】【php】curl报错,错误代码77,CURLE_SSL_CACERT_BADFILE (77)解决方法
CURLE_SSL_CACERT_BADFILE (77) - 读取 SSL CA 证书时遇到问题(可能是路径错误或访问权限问题) 在微信接口相关开发时容易出现此问题 这一般是因为服务更新了相关的软件 ...
- Impala的分布式查询
翻译自<Getting Started with Impala> 分布式查询 分布式查询是impala的核心.曾几何时,你需要研究并行计算,才能开始进行深奥而晦涩的操作.现在,有运行在Ha ...
- jmeter压力测试入门
http://www.51testing.com/html/80/n-853680.html http://blog.csdn.net/vincy_zhao/article/details/70238 ...
- Virtualbox虚拟机相关
Virtualbox虚拟机相关 Virtualbox是我一直使用的虚拟机,由于需要一些测试环境等,会经常使用多个虚拟机.经常捣腾.之前有涉及到一些virtualbox方面的问题的处理,并没有记录下来, ...
- HDU 3879 Base Station
Base Station Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...
- Leetcode 330.按要求补齐数组
按要求补齐数组 给定一个已排序的正整数数组 nums,和一个正整数 n .从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数 ...