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. MySQL主主复制+LVS+Keepalived实现MySQL高可用性

    http://bestvivi.com/2015/09/09/MySQL%E4%B8%BB%E4%B8%BB%E5%A4%8D%E5%88%B6+LVS+Keepalived%E5%AE%9E%E7% ...

  2. vim的一些高级配置

    今天有幸看到一篇博文,有一些vim的高级配置 在linux或者unix下面的.vimrc文件中,在其中可以添加如下片段,可以实现解释上面你说的那些高级用法 " Ctrl + K 插入模式下光 ...

  3. 使用TabLayout快速实现一个导航栏

    在没有Material Design的年代,要实现一个类似微信主页面的效果,我们有以下几种解决方案: 1.Fragment + ViewPager  +  RadioGroup自定义固定导航条 2.F ...

  4. Android(java)学习笔记149:Android线程形态之 AsyncTask (异步任务)

    1. AsyncTask和Handler的优缺点比较: 1)AsyncTask实现的原理和适用的优缺点        AsyncTask是Android提供的轻量级的异步类,可以直接继承AsyncTa ...

  5. 提升资源利用率的MapReduce框架

    Hadoop系统提供了MapReduce计算框架的开源实现,像Yahoo!.Facebook.淘宝.中移动.百度.腾讯等公司都在借助 Hadoop进行海量数据处理.Hadoop系统性能不仅取决于任务调 ...

  6. webbreswer

    为了帮助网友解决"怎么用C#的webBrowser模拟点击页面上的标签"相关的问题,中国学网通过互联网对"怎么用C#的webBrowser模拟点击页面上的标签" ...

  7. sklearn两种保存模型的方式

    作者:卢嘉颖 链接:https://www.zhihu.com/question/27187105/answer/97334347 来源:知乎 著作权归作者所有,转载请联系作者获得授权. 1. pic ...

  8. (转)了解了这些才能开始发挥jQuery的威力

    原文地址:http://www.cnblogs.com/dolphinX/p/3347677.html 由于当前jQuery如此的如雷贯耳,相信不用介绍什么是jQuery了,公司代码中广泛应用了jQu ...

  9. JS实现跳转到页面任何地方

    要实现两个内容: 1.从A页面跳转到B页面任何地方 方法:用id对要跳转的地方进行标记. 首先,在A页面可以设一个链接 <a href = "b.html#pos" targ ...

  10. datejs lib

    // Get today's date Date.today(); // Add 5 days to today Date.today().add(5).days(); // Get Friday o ...