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. HDFS Architecture--官方文档

    HDFS Architecture Introduction The Hadoop Distributed File System (HDFS) is a distributed file syste ...

  2. 网络接口 使用NSURLConnection完成Get和Post方法

    网络接口 使用NSURLConnection完成Get和Post方法 什么是URL: URL就是统一资源定位器(UniformResourceLocator:URL).通俗地说,它是用来指出某一项信息 ...

  3. WWDC心愿单:新版OS X或将有这些变化

    每年的WWDC开发者大会上苹果都会展示最新的软件研发成果,在新一代MacBook Pro和MacBook Air笔记本到来之前,我们不妨来期待下新版OS X会有怎样的变化. Siri入驻Mac   此 ...

  4. Notification封装(没做从网络下载)

    1.Notification作为消息通知,这里简单封装了使用 建立一个bean package com.jcut.view; /** * Author:JsonLu * DateTime:2016/2 ...

  5. Python的基本配置

    Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结 ...

  6. Linux下登陆mysql服务器不需要输入账号密码信息

    linux下登录mysql服务器一般都是在命令行手动输入链接信息 [root@localhost ~]# mysql -hlocalhost -uroot -p11111 而在mysql 5.6之后版 ...

  7. 抓取锁的sql语句-第二次修改

    CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000);  --定义 v_sql 接受抓取锁的sql语句 CUR_LOCK SYS ...

  8. 对C#泛型实例化对像--转

    最近在编写一套开发框架结构主要应用.Net 3.5以上的框架开发与应用.在此框架中应用了较多的泛型.下面来讲讲对泛型的实例化,以代码为例,如: public class A { } public cl ...

  9. 【USACO 2.1.5】海明码

    [题目描述] 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个 ...

  10. windows 7下安装python+mongodb

    1. python安装 下载:http://python.org/download/ 直接双击安装,安装完后将路径加入系统环境变量path中. 2. mongodb安装 下载:http://www.m ...