Leetcode 之Longest Palindromic Substring(30)

很经典的一道题,最长回文子串,有多种方法。
首先介绍的一种方法是从中间向两边展开。注意区分aba和abba型的回文串;如果当前最长的子串已经当于两边中最长的子串了,则无需再去判断。
//从中间向两边展开
string expandAroundCenter(string s, int c1, int c2)
{
int l = c1, r = c2;
int n = s.length(); while (l >= && r <= n - && s[l] == s[r])
{
l--;
r++;
}
return s.substr(l + , r - l - );
} string longestPalindString(string s)
{
int n = s.length();
if (n == )return s; string longest = s.substr(, );
int mid = n / ;
//
for (int i = ; i < n - ; i++)
{
//如果剩下的字串长度已经小于当前最大长度字串的长度,则无需再比较
if ( * (mid - i + ) < longest.length() && * (n - mid - i)<longest.length())
break;
//如果是aba型的回文串
string p1 = expandAroundCenter(s, mid+i, mid+i);
if (p1.length()>longest.length())
longest = p1; string p2 = expandAroundCenter(s, mid - i, mid - i);
if (p2.length()>longest.length())
longest = p2;
//如果是abba型的回文串
string p3 = expandAroundCenter(s, mid+i, mid+i + );
if (p3.length()>longest.length())
longest = p3; string p4 = expandAroundCenter(s, mid - i, mid - i - );
if (p4.length()>longest.length())
longest = p4;
} return longest;
}
还有一种动态规划的方法,第一次接触,还需慢慢理解。
string longestPalindString(string s)
{
const int n = s.size();
bool f[n][n]; fill_n(&f[][], n*n, false); int max_length = , start = ; for (int i = ; i < n; i++)
{
f[i][i] = true;
for (int j = ; j < i; j++)
{
f[j][i] = (s[j] == s[i] && (i - j < || f[j + ][i - ]));
if (f[j][i] && max_length < (i - j + ))
{
max_length = i - j + ;
start = j;
}
}
}
s.substr(start, max_length);
}
Leetcode 之Longest Palindromic Substring(30)的更多相关文章
- 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 ...
- 求最长回文子串 - 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 ...
- 【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][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- [LeetCode] 5. 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
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
随机推荐
- BZOJ3156: 防御准备 【斜率优化dp】
3156: 防御准备 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2207 Solved: 933 [Submit][Status][Discu ...
- extjs gridpanel 操作行 得到选中行
extjs gridpanel 操作行 得到选中行的列 在Extjs 3.2.0上适合 var model = grid.getSelectionModel(); model.selectAll(); ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- 自己做的jquery的autocomplete的一个例子
转载自:http://dada-fangfang.iteye.com/blog/695464 首先下载jquery.js和jquery.autocomplete.js 注意:jquery.js 要放在 ...
- 查看自己电脑外网IP
连着wifi,在CMD窗口中显示的无线局域网适配器IP很有可能是内网IP.此时可以通过下面方法查看自己的电脑外网IP. 1.前提条件可以上外网: 2.上外网百度,输入IP,进行搜索. 3.查看结果即可 ...
- 在MVC5中使用Ninject 依赖注入
各大主流.Net的IOC框架性能测试比较 : http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html 使用NuGet ...
- PHP扩展--Yaf框架安装
安装/配置 编译安装 wge thttp://pecl.php.net/get/yaf-2.3.5.tgz tar -zxvfyaf-2.3.5.tgz cd yaf-2.3.5/ cd extens ...
- salt总结
安装jdk jdk: file.managed: - source: salt://service/zabbix/files/jdk1.8.0_121.tar.gz - name: /usr/loca ...
- 【BZOJ2288】生日礼物 [贪心]
生日礼物 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description ftiasch 18岁生日的时候, ...
- 【BZOJ3191】【JLOI2013】卡牌游戏 [DP]
卡牌游戏 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description N个人坐成一圈玩游戏.一开始我 ...