/*
* 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. linux-ngnix服务(一)

    httpd MPM: prefork:进程模型,两级结构,主进程master负责生成子进程,每个子进程负责响应一个请求 worker:线程模型,三级结构,主进程master负责生成子进程,每个子进程负 ...

  2. python--内置常用模块

    一 . 简单的了解模块 你写的每一个py文件都是一个模块. 还有一些我们一直在使用的模块. buildins  内置模块. print, input. random  主要是和随机相关的的内容 ran ...

  3. Python Hashlib笔记

    #python3.4hashlib module - A common interface to many hash functions.hash.digest() - Return the dige ...

  4. 【10】css hack原理及常用hack

    [10]css hack原理及常用hack 原理:利用不同浏览器对CSS的支持和解析结果不一样编写针对特定浏览器样式.常见的hack有1)属性hack.2)选择器hack.3)IE条件注释 IE条件注 ...

  5. 【02】你是如何理解 HTML 语义化的,有什么好处

    [02]你是如何理解 HTML 语义化的   01,语义化,就是通过HTML标签来表示页面包含的信息. 02,其中有HTML标签的语义化和CSS命名的语义化. 03,HTML标签语义化的的含义是:   ...

  6. luogu2805 [NOI2009]植物大战僵尸

    想象一下,要搞掉一个植物,必须先搞掉另一些植物--我们可以发现这是一个最大权闭合子图的问题. 最大权闭合子图的话,太空飞行计划问题是一个入门题,可以一看. 然而我们手玩一下样例就会惊恐地发现,保护关系 ...

  7. app启动画面(prepo)

    IPhone启动画面以及图标的设置 目前IPhone的分辨率为:320X480.640X960.640X1136. Default.png                    320X480 iPh ...

  8. iphone数据库(sqlite3)的用法操作oc,xcode

    首先你在用之前要在项目中加入libsqlite3.dylib 1.定义模型 [cpp] #import <Foundation/Foundation.h>   #import " ...

  9. Oracle跟踪分析数据库启动的各个阶段

    目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...

  10. LINQ-查询表达式基础

    一.LINQ查询的数据源 从应用程序的角度来看,原始源数据的特定类型和结构并不重要. 应用程序始终将源数据视为 IEnumerable<T> 或 IQueryable<T> 集 ...