5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of Sis 1000, and there exists one unique longest palindromic substring.
==
class Solution {
public:
string longestPalindrome(string s) {
/**bool dp[i][j]代表s[i-j]是否回文
dp[i][j] = true ;i==j
= s[i]==s[j] ;j=i+1
= (s[i]==s[j])&& dp[i+1][j-1]; j>i+1
要记住,dp[i][j]表示的i-j之间的字符串是不是回文,
当i==j时,dp[i][i]当然是回文
当j=i+1时,dp[i][j],要看字符串s[i]和s[j]之间是不是相同的?
当j>i+1时,dp[i][j]的判定情况,s[i]s[j]和dp[i+1][j-1]共同决定的; 怎么开始的?
外层循环j控制,
内存循环i判断当前能到达的位置是否是回文;
*/
int n = s.size();
bool dp[n][n];
fill_n(&dp[][],n*n,false);///初始化
int max_len = ;
int start = ; for(int j = ;j<s.size();j++){
for(int i = ;i<=j;i++){
if(i==j) dp[i][j] = true;
else if(j==(i+)) dp[i][j] = s[i]==s[j]?true:false;
else if(j>(i+)) dp[i][j] = s[i]==s[j]&&dp[i+][j-];
if(dp[i][j]&&max_len < (j-i+)){
max_len = j-i+;
start = i;
}
}///for
}
return s.substr(start,max_len);
}
};
5. Longest Palindromic Substring的更多相关文章
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- [LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...
- leetcode-【中等题】5. Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- Valid Palindrome ---- LeetCode 125
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- (转载)android炫酷实用的开源框架(UI框架)
可以实现一些场常用炫酷效果,包含android-lockpattern(图案密码解锁).Titanic(可以显示水位上升下降的TextView).Pull-to-Refresh.Rentals-And ...
- c++11新的小猫腻
1.void*指针的使用,平时见得也很多了,至于为什么使用void* 指针,很多人有自己的见解,反正普通指针轻轻松松的转向void * 指针,但是void*指针转向其他的指针都要采用强制转换的. 2. ...
- C#通过WatiN操作页面中内嵌的Iframe
通过WatiN.Core.Broswer.Frame()方法来获取iframe对象,之后的容器就是frame,然后进行操作. 下面的例子是登录QQ空间的: Frame frame = browser. ...
- C#中隐藏(new)、方法重写(override)、重载(overload)的区别
转自:http://www.cnblogs.com/glife/archive/2009/12/28/1633947.html 重载.重写和隐藏的定义: 重载:public string ToStri ...
- 让LinqToSQL使用Web.Config中的链接字符串(修改Settings.Designer.cs)
[global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.Debug ...
- 带head的gridview
这是github上的一个项目,根据谷歌的那个HeadGridView改的,因为谷歌的那个addHeadView后宽度不能填充屏幕,下面是代码. 来源:https://github.com/liaohu ...
- android 手机屏幕有关的几个工具(屏幕宽高,dp和px互相转换)
平时适配页面时经常会需要根据屏幕的宽高来设置控件的大小,很多时候在代码中还会需要dp和px互相转换. 今天把最常用的几个记录一下,经测试包括2.3 ~ 5.0之间的版本都可用,其他版本未测,不过应该也 ...
- IC卡复位应答ATR的数据元和它们的意义
ISO/IEC 7816-3标准中对ATR的数据串和数据元做了规定和描述.ATR的数据元和它们的意义: 数据元 说明 TS 起始字符 T0 格式字符 TA1,TB1,TC1,TD1,... 接口字符 ...
- 3.Complementing a Strand of DNA
Problem In DNA strings, symbols 'A' and 'T' are complements of each other, as are 'C' and 'G'. The r ...