【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.
解题分析:
之前百度实习生面试也被问到这个问题。这个题比较简单。就是在分别考虑对称字符字串为奇数和偶数的情况,在不超过字符串范围的情况下向两边扩展,找到最大值,并记录下最大子串的位置,最后返回即可。
具体代码:
public class Solution {
public static String longestPalindrome(String s) {
if(s==null)
return null;
if(s.length()==0)
return "";
if(s.length()==1)
return s;
if(s.length()==2){
if(s.charAt(0)!=s.charAt(1)){
return s.substring(1);
}
else
return s;
}
int max=1;
int start=0;
int end=0;
char[] array=s.toCharArray();
//对称字符字串长度为数的情况
for(int i=1;i<s.length()-1;i++){
int from=i;
int to=i;
// 要保证不超过字符数组边界的情况下,待扩展的两字符相等
while(from>=0&&to<s.length()&&array[from]==array[to]){
from--;
to++;
}
int len=to-from-1;
if(len>max){
start=from+1;
end=to-1;
max=len;
}
}
//对称字符字串长度为偶数的情况
for(int i=0;i<s.length()-1;i++){
if(array[i]!=array[i+1])
continue;
int from=i;
int to=i+1;
while(from>=0&&to<s.length()&&array[from]==array[to]){
from--;
to++;
}
int len=to-from-1;
if(len>max){
start=from+1;
end=to-1;
max=len;
}
}
return s.substring(start,end+1);
}
}
【leetcode】5. Longest Palindromic Substring的更多相关文章
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【LeetCode】005. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 【一天一道LeetCode】#5 Longest Palindromic Substring
一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- 【leetcode】516. Longest Palindromic Subsequence
题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- UVALive 7070 The E-pang Palace 暴力
The E-pang Palace Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem ...
- CDOJ 481 Apparent Magnitude 水题
Apparent Magnitude Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/sh ...
- ZJU-PAT 1065. A+B and C (64bit) (20)
Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Input S ...
- vector<int> v2 = 42; 为何非法
C++ Primer 第四版,第十三章“复制控制” 习题13.2,为何vector<int> v2 = 42; 不能编译? 百度贴吧里的一位楼主给出了答案,本人认为正确,特此引用: 参考链 ...
- JellyViewPager
https://github.com/jfeinstein10/JazzyViewPager https://github.com/chiemy/JellyViewPager JellyViewPag ...
- iOS开发——语法篇OC篇&高级语法精讲
高级语法精讲 一.NSSet.NSMutableSet集合的介绍 1)NSSet.NSMutableSet集合,元素是无序的,不能有重复的值. 2)用实例方法创建一个不可变集合对象 例如: //宏定义 ...
- android145 360 进程管理
package com.itheima.mobileguard.activities; import java.util.ArrayList; import java.util.List; impor ...
- SHELL 详解
http://blog.csdn.net/vah101/article/details/6173488 ( a=2;b=4;c=9; ) 子shell 环境 { a=2;b=4;c=9; } 当前sh ...
- sizeWithFont 不是线程安全。
在ios开发中经常使用用sizeWithFont 方法来计算UILabel 的frame, 例如动态计算UITableViewCell 的高度,在主线程处理没有问题,但是在子线程用此方法来计算就会出现 ...
- 自动监控主从MySQL同步的SHELL脚本
代码如下: #!/bin/bash #check MySQL_Slave Status #crontab time 00:10 MYSQLPORT=`netstat -na|grep "LI ...