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 2.6.32

    http://www.cnblogs.com/xcywt/p/4998963.html 系统定义符号集中声明在 /usr/src/kernels/linux-2.6.32/include/linux/ ...

  2. 【转】VS2010中使用AnkhSvn

    今天想到要在自己的开发环境IDE(Visual Studio 2010)中安装一个代码管理器的插件,本人在使用VS2005的时候一直都是使用AnkhSvn-2.1.7444.278这版本,使用过程中也 ...

  3. 嵌入式Qt4.7.1安装详解

    嵌入式Qt 4.7.1安装移植过程详解环境:Ubuntu 12.04VMware 9.0 qt软件包:qt-everywhere-opensource-src-4.7.1.tar.gz (飞凌自带) ...

  4. android开发之ExpandableListView的使用,实现类似QQ好友列表

    由于工作需要,今天简单研究了一下ExpandableListView,做了一个类似QQ列表的Demo,和大家分享一下. 效果图如下: 先来看看主布局文件: <RelativeLayout xml ...

  5. Android_layout_note

    LinearLayout线程布局 LinearLayout属性 android:orientation表示线性布局的方向 vertical: 垂直.从上往下 horizontal: 水平.从左往右 a ...

  6. ffmpeg之YUYV转RGB ARM使用流程分析

    本例基于3.2.2 ffmpeg 一.应用调用API 二.头文件包含的API接口 对应于libswscale.so.libswscale.so.4.libswscale.so.4.2.100中 sws ...

  7. Spring、struts、webwork2三者MVC的比较

    http://blog.sina.com.cn/s/blog_4a69fa43010005il.html 在web应用方面,Spring有独立的MVC实现,与struts和webwork2相比毫不逊色 ...

  8. JS实现各种页面的刷新

    JS实现各种页面的刷新功能 1.刷新当前页面 opener.location.replace(opener.location.href); 或者window.opener.window.locatio ...

  9. android 6.0特性翻译 --渣渣

    所有关于Android 6.0 棉花糖的知识 上下文帮助 1.现在按压:不需要离开你正在运行的app或者访问的网站就可 获取帮助,仅仅触摸和按下Home按钮.(长按Home键,可以在 android ...

  10. 第三篇:gradle 编译 Android app 概览

    引言:经过上两篇的论述,我们已经从代码到架构都简单的熟悉了一遍,理论上,只要知道android app的编译过程,我们大可以自己写一份用gradle编译app的插件,插件内将将整个流程用Task的依赖 ...