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. Java实现折半(二分)插入排序

    /*折半插入查找思想:每趟将一个带排序的元素作为关键字插入到已经排好的部分序列的适当位置上,查找适当位置的方法用折半查找法 * 适合记录数较多的场景 * 在查找插入位置时节省了时间 * 在记录移动次数 ...

  2. 导出EXCEL数据时防止数值变科学计数的办法

    网上有很多说法,最简单直接正确的做法是判断一下是否为数值以及长度,然后给单元格加上以下CSS即可: mso-generic-font-family:auto;   mso-font-charset:1 ...

  3. Scala闭包

    假如我们定义如下的函数: (x:Int) => x + more 这里我们引入一个自由变量more.它不是所定义函数的参数,而这个变量定义在函数外面,比如: var more =1 那么我们有如 ...

  4. C# ADO.NET参数查询

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 使用的 SQL Server 版本不支持数据类型“datetime2”解决办法

    不论下方提示什么数据格式有错误,一般都是entity生成的时候的问题.比如服务器上用的sql2005,自己用的2008. 解决方法: model层生成的model.edmx文件,用记事本打开, 将&l ...

  6. UML 结构图之包图 总结

    [注] 本文不是包图的基础教程, 只是包图的图形总结. 学习UML图形 推荐阅读<UML参考手册>第2版. http://www.umlchina.com/ 推荐微软的开发软件设计模型 h ...

  7. linq 多条件查询 where 拼接+分页

    首先定义一个静态类 public static class QueryAssembly { /// <summary> /// 返回true /// </summary> // ...

  8. IOS高级开发 runtime(一)

    一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...

  9. sql中truncate 、delete与drop区别

    SQL truncate .delete与drop区别   相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL ...

  10. HDU 3008 Warcraft(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3008 题目大意:人有100血和100魔法,每秒增加 t 魔法(不能超过100).n个技能,每个技能消耗 ...