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.

题目大意:给一个字符串S,存在唯一的最长的回文子串,求这个回文子串。

解题思路:首先这个题有O(n)的解,是在http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html这里有详细的解释,本文的算法是O(n^2)的,中规中矩的做法。回文有两种,abba和aba,本文分别对子串以i为中心进行检查偶数和奇数哪个长,同时记录长度,下标,奇偶等信息。

Talk is cheap>>

  public String longestPalindrome(String s) {
if (s == null || s.length() <= 1) {
return s;
}
int max_len = 0, pos = 0;
boolean odd = false;
for (int i = 0; i < s.length(); i++) {
int forward = i - 1;
int backward = i + 1;
int len = 0;
while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
forward--;
backward++;
len++;
}
if (max_len < (len * 2 + 1)) {
max_len = len * 2 + 1;
odd = true;
pos = i;
}
len = 0;
forward = i;
backward = i + 1;
while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
forward--;
backward++;
len++;
}
if (max_len < len * 2) {
max_len = len * 2;
odd = false;
pos = i;
}
}
if (odd) {
return s.substring(pos - (max_len - 1) / 2, pos + (max_len - 1) / 2 + 1);
}
return s.substring(pos - max_len / 2 + 1, pos + max_len / 2 + 1);
}

Longest Palindromic Substring——LeetCode的更多相关文章

  1. Longest Palindromic Substring -LeetCode

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

  2. Longest Palindromic Substring leetcode java

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

  3. [LeetCode] Longest Palindromic Substring 最长回文串

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

  4. Leetcode Longest Palindromic Substring

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

  5. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  6. LeetCode 5 Longest Palindromic Substring(最长子序列)

    题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...

  7. 【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 ...

  8. [LeetCode] Longest Palindromic Substring(manacher algorithm)

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

  9. leetcode:Longest Palindromic Substring(求最大的回文字符串)

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

随机推荐

  1. InnoDB主要数据结构及调用流程

    InnoDB主要数据结构及调用流程 InnoDB是MySQL中常用的数据引擎.本文将从源码级别对InnoDB重点数据结构和调用流程进行分析. 主要数据结构(buf0buf.h) Buf_pool Bu ...

  2. mysql源码分析

    http://blog.csdn.net/u012935160/article/category/2697485

  3. [转] [翻译]图解boost::bind

    http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...

  4. WAMP 环境下,YII创建失败 提示 "'php.exe' 不是内部或外部命..."

    现象: http://www.yiichina.com/guide/quickstart.first-app 使用这里的命令  % YiiRoot/framework/yiic webapp WebR ...

  5. Android中使用HttpGet和HttpPost访问HTTP资源

    需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 public String getResultFo ...

  6. Have trouble in your life

    当你烦恼的时候不知道如何是好时,你可以下载此程序,可以帮助你化解烦恼! 下载地址: http://pan.baidu.com/s/1i3FtxHF

  7. ASP.NET C#使用JavaScriptSerializer实现序列化与反序列化得到JSON

    在JavaScriptSerializer中,我们可以看到下面可以使用的方法或者构造函数,它们都是实例方法: Member Description JavaScriptSerializer() 构造函 ...

  8. leetcode修炼之路——350. Intersection of Two Arrays II

    题目如下: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2 ...

  9. [转] iOS SDK:iOS调试技巧

    原文:  http://www.cocoachina.com/ios/20130517/6225.html 为什么你的数组包含3个项目而不是5个?为什么你的游戏运行缓慢?这些都跟调试有关,调试是开发过 ...

  10. 关于NSURL的一些属性的记录

    关于NSURL的一些属性的记录 NSLog(@"%@", request.URL.absoluteString); NSLog(@"%@", request.U ...