Java [leetcode 5] 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.
解题思路:
遍历方法,时间复杂度为O(n)。首先从字符串的开头位置一直往后遍历,在每次遍历的过程中由该位置向两边扩散,直到找到最长的子回文串为止。同时需要考虑奇字符串和偶字符串的情况。
代码如下:
public class Solution {
public String longestPalindrome(String s) {
int n = s.length();
String longest = s.substring(0, 1); if (s == null || s.length() == 0)
return null;
for (int i = 0; i < n; i++) {
String p1 = expandFromCenter(s, i, i);
if (p1.length() > longest.length())
longest = p1;
String p2 = expandFromCenter(s, i, i + 1);
if (p2.length() > longest.length())
longest = p2;
}
return longest;
} public String expandFromCenter(String s, int c1, int c2) {
int head = c1;
int tail = c2;
int m = s.length(); while (head >= 0 && tail < m && s.charAt(head) == s.charAt(tail)) {
head--;
tail++;
}
return s.substring(head + 1, tail);
}
}
Java [leetcode 5] Longest Palindromic Substring的更多相关文章
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- 【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 ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
随机推荐
- C# \uxxx Unicode编码解码
/// <summary> /// Unicode编码 /// </summary> /// <param name="str"></pa ...
- uva 11609
可以想到 答案为 1*C(1,n)+2*C(2,n)+3*C(3,n)+....+n*C(n,n); 由公式 k*C(k,n) = n*C(k-1,n-1) 所以最终答案 n*2^(n-1) 用到快速 ...
- vsftp在REDHAT,CENTOS 5中登录慢的解决办法
vsftp在REDHAT,CENTOS 5中登录慢的解决办法 vsftp在REDHAT,CENTOS 5中不仅登录慢,至少花30秒左右,而且上传文件的速度也受影响, 经过摸索,根本原因在DNS解析上花 ...
- CPU/ABI显示No system images installed for this target的解决方案
CPU/ABI显示No system images installed for this target的解决方案 手动下载image http://www.androiddevtools.cn/ SD ...
- 220 DIV2 A. Inna and Pink Pony
Inna and Pink Pony 输入n,m,i,j,a,b 可以看成n行m列的矩阵,起点(i,j),每次移动(a,b),(-a,-b),(-a,b),(a,-b) 可移动到(1,m),(n,1) ...
- N皇后//搜索入门
P1080 N皇后 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列只有一个,每条 ...
- eclipse中clean操作中如何将validating除去
eclipse中去掉js validating方法:1. 删除.project文件中的 <buildSpec></buildSpec>中的:<buildCommand&g ...
- linux 线程的内核栈是独立的还是共享父进程的?
需要考证 考证结果: 其内核栈是独立的 206 static struct task_struct *dup_task_struct(struct task_struct *orig) 207 { 2 ...
- 利用python 获取 windows 组策略
工作中有时候会有这种需求: 1. 自动配置组策略的安全基线,这个东西不用你自己写了,微软有这个工具,Microsoft Security Compliance Manager,你可以在下面的地址去下载 ...
- Qt 添加外部库文件(四种方法)
Qt添加外部库文件, 一种就是直接加库文件的绝对路劲,这种方法简单,但是遇到多个库文件的时候,会很麻烦,而且,如果工程移动位置以后还需要重新配置 另一种就是相对路径了,不过Qt 编译的文件会在一个单独 ...