【LeetCode OJ】Longest Palindromic Substring

时间:2022-12-14 23:12:06

题目链接: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 &lpar;middle&rpar; 经典

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

  6. 【LeetCode每天一题】Longest Palindromic Substring&lpar;最长回文字串&rpar;

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

  7. Leetcode&colon;【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. 如何理解 卷积 和pooling

    转自:http://blog.csdn.net/malefactor/article/details/51078135 CNN是目前自然语言处理中和RNN并驾齐驱的两种最常见的深度学习模型.图1展示了 ...

  2. &lbrack;SQL Basics&rsqb; Indexes

    An index is used to speed up searching in the database. By default, when you create this table, your ...

  3. ServletContext与ServletConfig的详解及区别

    转自http://hi.baidu.com/huaxuelili/item/1704a03dbb5cd7f22784f4c6 一.ServletContext详解ServletContext是serv ...

  4. fFFmpeg 命令、案例、测试*

    gitbook: https://www.gitbook.com/book/xdsnet/other-doc-cn-ffmpeg/details ffmpeg [全局选项] {[输入文件选项] -i ...

  5. FIREDAC数据引擎

    以前使用过BDE.ADO.DBX等数据引擎,后来发现它们都没有UNIDAC好用, 所以在很长的一段时间内中间件都使用UNIDAC作为数据引擎. 偶然的机会,使用了DELPHI XE5自带的FIREDA ...

  6. linux svn服务器普通passwd和sasl2配置

    朋友昨天问我一个问题,他公司使用的vpn连接的svn,使用svn管理软件进行svn update是可行的,使用命令行svn update出错,svn status可行. 思路1: 刚开始我关注的焦点都 ...

  7. cordova的常用命令

    常用命令 npm install -g cordova // 加载cordovecordova create MyApp //创建一个新的文件夹cd MyApp //找到当前目录cordova pla ...

  8. 钟意Action

    package com.j1.mai.action; import com.github.pagehelper.PageInfo; import com.j1.app.mysql.model.Appr ...

  9. special points about git

    1 about "origin/master tracks the remote branch" 1.1 what does tracking mean? after " ...

  10. Unity3D 调用Java,Java调用Unity3D

    1.无返回值 AndroidJavaClass fee = new AndroidJavaClass("com.wiker.Test"); fee.CallStatic(&quot ...