原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/

1. 题目要求:找出字符串中的最大回文子串

2. 注意:要考虑回文子串中的字符个数是奇数还是偶数!!!

例如,“aabaa”是一个奇数个字符的回文字符串,他的中心只有一个字符“b”。

“aabbaa”是一个偶数个字符的回文字符串,他的中心却有两个相同字符“bb”

3. 思路:暴力解决,以每个字符为中心,对该字符的两边进行匹配,找出最长的回文子串1;再以两个相邻字符为中心,进行同样操作,找出最长回文子串2。然后对两个子串进行比较,取最长者。

 class Solution {
public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
} private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
}
}

LeetCode:5. Longest Palindromic Substring(Medium)的更多相关文章

  1. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  2. 5. Longest Palindromic Substring (DP)

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

  3. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  4. LeetCode:14. Longest Commen Prefix(Easy)

    1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...

  5. LeetCode:12. Integer to Roman(Medium)

    1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...

  6. Leetcode 之Longest Palindromic Substring(30)

    很经典的一道题,最长回文子串,有多种方法. 首先介绍的一种方法是从中间向两边展开.注意区分aba和abba型的回文串:如果当前最长的子串已经当于两边中最长的子串了,则无需再去判断. //从中间向两边展 ...

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

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

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

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

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

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

随机推荐

  1. 关于git的使用

    一.关于GIT Git --- The stupid content tracker, 傻瓜内容跟踪器.Linus Torvalds 是这样给我们介绍 Git 的.   Git 是用于 Linux内核 ...

  2. OpenCV人脸识别

    import cv2 filename = 'pic.jpg' def detect(filename): face_cascade = cv2.CascadeClassifier('./haarca ...

  3. LA 4043 最优匹配

    题目链接:https://vjudge.net/contest/161820#problem/A 题意: n 个 白点,n 个黑点,给出了坐标,求完美匹配后,各点不相交,输出白点对于的黑点编号:(输出 ...

  4. nodejs protobuff node-protobuf c++ windows扩展安装笔记

    https://www.npmjs.com/package/node-protobuf 按照作者所说的办法在windows平台安装的办法,先到google的github下载2.6.1版本的protob ...

  5. 字符ASCII转换

    实现效果: 关键知识: 实现代码: private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != stri ...

  6. 问题 A: B 继承 圆到圆柱体

    题目描述 定义了Circle圆形类,在此基础上派生出Cylinder圆柱体类. Circle圆形类定义如下: class Circle { public: Circle(double r):radiu ...

  7. focal loss和retinanet

    这个是自己用的focal loss的代码和公式推导:https://github.com/zimenglan-sysu-512/Focal-Loss 这个是有retinanet:https://git ...

  8. c#运用this.invoke() 在多线程时对UI进行修改

    什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括运行中的程序和程序所使用到的内存和系统资源.而一个进程又是由多个线程所组成的,线程是程序中的一个执行流,每个线程都有自己的专有寄存器(栈 ...

  9. linnx 修改ip地址

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 [编辑网卡的配置文件] 输入上述命令后回车,打开配置文件,使用方向键移动光标到最后一行,按字母键“i”,进入编 ...

  10. sqlite迁移mysql(导入导出数据)

    第一步,将数据导出 进入sqlite3->.open [打开文件路径]->.cd [要保存的路径]->.output [导出文件名字.sql]->.dump 等待导出成功后,就 ...