题目链接:https://leetcode.com/problems/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.

解题思路:palindromic-substring是指回文串,例如abba、abcba,它有一个特点就是从字符串的中间开始往两边的字符都是一样的。我们可以从字符串的第二个字符开始向左边和右边同时扫描,找到字符长度最长的回文串。示例代码如下:

public class Solution
{
public String longestPalindrome(String s)
{
int n = s.length();
if (n <= 1)
return s;
int maxlen = 1, k, j, a = 0;
int l;
for (int i = 1; i < n;)
{
k = i - 1;
j = i + 1;
//扫描左边与s[i]相同的字符
while (k >= 0 && s.charAt(k) == s.charAt(i))
k--;
//扫描右边与是s[i]相同的字符
while (j < n && s.charAt(j) == s.charAt(i))
j++;
while (k >= 0 && j < n && s.charAt(k) == s.charAt(j))
{
k--;
j++;
}
l = j - k - 1;
if (maxlen < l)
{
a = k + 1;
maxlen = l;
}
i++;
}
return s.substring(a, a+maxlen);
}

【LeetCode OJ】Longest Palindromic Substring的更多相关文章

  1. LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

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

  2. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  3. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  4. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  5. 【leetcode】Longest Palindromic Substring (middle) 经典

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

  6. 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)

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

  7. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  8. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

  9. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

随机推荐

  1. 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查

    第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...

  2. e778. 在JList中加入和删除项

    The default model for a list does not allow the addition and removal of items. The list must be crea ...

  3. TI单节电量计基本介绍及常见问题解答

    电量计(gas gauge /fuel gauge)是用来计量显示电池电量,通常包括mAh剩余容量(RM),满充容量(FCC),百分比容量(SOC),电压,电流,温度等,部分电量计还包含放空,充满时间 ...

  4. 使用iftop监控网卡实时流量

    Iftop工具主要用来显示本机网络流量情况及各相互通信的流量集合,如单独同哪台机器间的流量大小,非常适合于代理服务器和iptables服务器使用,这样可以方便的查看各客户端流量情况.iftop可以在类 ...

  5. php 统计fasta 序列长度和GC含量

    最近php7的消息铺天盖地, 忍不住想尝试下.星期天看了下语法, 写个小脚本练下手: 这个脚本读取fasta 文件, 输出序列的长度和GC含量: <?php $fasta = "tes ...

  6. winform中文本框,软键盘跟随

    private void textBox1_Click(object sender, EventArgs e) { //Control.MousePosition Point p = System.W ...

  7. c#基础操作

    内网 IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[];//获得当前IP地址 string ip = ipAddr.ToS ...

  8. Drools 语法

    Drools 语法 规则语法 package: package 的名字是随意的,不必必须对应物理路径 import: 导入外部变量 规则的编译与运行要通过Drools 提供的各种API 来实现.API ...

  9. springMVC工程使用jreloader实现热部署

    springMVC工程使用jreloader实现热部署applicationContext - ContextLoaderListener重新加载DispatcherServlet 重新加载提高开发效 ...

  10. 【转】IOS 学习之 NSPredicate 模糊、精确、查询

    转自:http://blog.csdn.net/lianbaixue/article/details/10579117   简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于S ...