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.

time=255ms
   accepted 暴力遍历

public String longestPalindrome(String s){
String result=null;
int length=s.length();
int max=0;
if(length==0)
return null;
else if(length==1)
return s;
else{
for(int i=1;i<length;i++){
boolean b=false;
int k=0;
int j=0;
for(int m=i-1;m>=0&&m>=i-2;m--){
if(s.charAt(i)==s.charAt(m)){
k=m-1;
b=true;
j=i+1;
while(b&&k>=0&&j<length){
if(s.charAt(j)==s.charAt(k)){
j++;
k--;
}else
b=false;
}
if(max<j-k-1){
result=s.substring(k+1,j);
max=j-k-1;
}
}
}
if(j>=length)
break;
}
return result;
} }

leetcode 第五题 Longest Palindromic Substring (java)的更多相关文章

  1. leetcode第五题--Longest Palindromic Substring

    Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...

  2. Leetcode: Longest Palindromic Substring. java

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

  3. leetcode--5 Longest Palindromic Substring

    1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...

  4. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  5. LeetCode(5)Longest Palindromic Substring

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

  6. leetcode 5. Longest Palindromic Substring [java]

    public String longestPalindrome(String s) { String rs = ""; int res = 0; for(int i = 0; i& ...

  7. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

  8. LeetCode第五题:Longest Palindromic Substring

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

  9. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

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

随机推荐

  1. linux 修改文件时间

    1.ls -l *.sh 2.touch -d "10/13/2013" *.sh [我想把所以的.sh文件修改到三个月前(2013年10月13)的时间.]3.ls -l *.sh ...

  2. 设置background属性使用selector的时候内置?attr报错的解决方案

    当我们设置background属性的时候可以设置background="@color/black" 也可以设置 background="@drawable/selecto ...

  3. java 中的set方法和get方法的理解

    get的意思是获取,set的意思是设置. get方法和set方法是实现类的封装访问的很好的工具. 当类中的变量设为private 时,他的意思就是说,只能通过自身和子类的访问,但是对于别的其他的类来说 ...

  4. 亿能测试白盒安全测试模板V1.0发布

    亿能测试白盒安全测试模板V1.0发布http://automationqa.com/forum.php?mod=viewthread&tid=2911&fromuid=21

  5. WebService 实现BS环境与BS环境传递参数,根据参数生成txt文档

    客户端: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Client.as ...

  6. C++ notes for beginners

    作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...

  7. PHP + ajax 实现异步登录验证

    login.html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  8. 在Linux下开始C语言的学习

    为什么要在linux下学习C语言? linux下可以体验到最纯粹的C语言编程,可以抛出其他IDE的影响 环境配置简单,一条命令就足够.甚至对于大多数linux发行版本,都已经不需要配置C语言的环境 查 ...

  9. document.all的用法详解

    all[] 已经被 Document 接口的标准的 getElementByid() 方法和 getElementsByTagName() 方法以及 Document 对象的 getElementsB ...

  10. (转)MyEclipse2014配置Tomcat开发JavaWeb程序JSP以及Servlet

    1.安装准备 1).下载安装MyEclipse2014,这已经是最新版本. 2).下载Tomcat 官网:http://tomcat.apache.org/ 我们选择8.0: http://tomca ...