/*
* 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的更多相关文章

  1. G.Longest Palindrome Substring

    链接:https://ac.nowcoder.com/acm/contest/908/G 题意: A palindrome is a symmetrical string, that is, a st ...

  2. [LeetCode] Longest Palindrome Substring 具体分析

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【算法】最长回文子串 longest palindrome substring

    对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. ...

  4. LeetCode the longest palindrome substring

    回文检测,参考http://blog.csdn.net/feliciafay/article/details/16984031 使用时间复杂度和空间复杂度相对较低的动态规划法来检测,具体的做法 图一 ...

  5. 最长回文字串 (The longest palindrome substring)

    这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...

  6. Leetcode: Longest Palindromic Substring && Summary: Palindrome

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  7. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

随机推荐

  1. shell脚本,一个经典题目。

    [root@localhost wyb]# cat zhuijiu.sh #!/bin/bash #.写一个脚本执行后,输入名字,产生随机数01-99之间的数字. #.如果相同的名字重复输入,抓到的数 ...

  2. 用vue 简单写的日历

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. LeetCode101--对称二叉树

    ''' 给定一个二叉树,检查它是否是镜像对称的. ''' class TreeNode: def __init__(self, x): self.val = x self.left = None se ...

  4. bat 获取命令执行后的多个返回值,并取最后一个

    最近在使用bat,遇到了这样的问题,获取adb shell cat命令之后的所有返回值,查了很长时间,才找到,现分享给大家. 举例如下: @for /f "tokens=*" %% ...

  5. python基础学习笔记——内置函数

    一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内 ...

  6. spring cloud 学习资源

    1.https://blog.csdn.net/column/details/17737.html 2.https://blog.csdn.net/column/details/15197.html? ...

  7. pytion3--文档字符串:__doc__

    除了#注释外,Python也支持可自动附加在对象上的文档,而且在运行时还可保存查看.从语法上来说,这类注释是写成字符串,放在模块文档.函数以及类语句的顶端.就在任何可执行程序代码前(#注释在其前也没问 ...

  8. BestCoder Round #36

    HDU5198 Strange Class 问题描述 在Vivid的学校里,有一个奇怪的班级(SC).在SC里,这些学生的名字非常奇怪.他们的名字形式是这样的anbncn(a,b,c两两不相同.).例 ...

  9. TeraTerm设定(解决日文乱码问题)

    首先,字体Font的MS Gothic是有Japanese的,设置为这个比较保险. 其次,在General Setup里将Language设为:English. 原理是什么我也不清楚,试了几个选择,就 ...

  10. 数字串(codevs 1394)

    题目描述 Description 给你一个长度为n的数字串,数字串里会包含1-m这些数字.如果连续的一段数字子串包含了1-m这些数字,则称这个数字字串为NUM串.你的任务是求出长度最短的NUM串是什么 ...