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.
答案
一道动规题目,每次用当前位置columnIndex跟前面位置rowIndex去比较时,如果相同的话,就去看columnIndex- 1和rowIndex+1字母的状态,其实在走到columnIndex时,columnIndex-1和它自己前面的字母是比较过了的,将这个状态记下来就行。
讲真,我这代码效率不高,应该还有其他思路,再想想。
代码
#define MAX_LENGTH 1001
class Solution {
public:
string longestPalindrome(string s) {
if(s.empty())
{
return string("");
}
bool charRelFlag[MAX_LENGTH][MAX_LENGTH];
int sLen = s.size();
int subStart = ;
int subEnd = ;
int rowIndex,columnIndex; for(rowIndex = ; rowIndex < sLen; ++ rowIndex)
{
charRelFlag[rowIndex][rowIndex] = true;
for(columnIndex = rowIndex + ; columnIndex < sLen; ++ columnIndex)
{
charRelFlag[rowIndex][columnIndex] = false;
}
} int maxLen = ;
for(columnIndex = ; columnIndex < sLen; ++ columnIndex)
{
for(rowIndex = ; rowIndex < columnIndex; ++ rowIndex)
{
if(s[rowIndex] == s[columnIndex])
{
if(rowIndex + == columnIndex)
{
charRelFlag[rowIndex][columnIndex] = true;
if(maxLen < )
{
maxLen = ;
subStart = rowIndex;
subEnd = columnIndex;
}
}else
{
if(charRelFlag[rowIndex + ][columnIndex - ] == true)
{
charRelFlag[rowIndex][columnIndex] = true;
if(maxLen < columnIndex - rowIndex + )
{
maxLen = columnIndex - rowIndex + ;
subStart = rowIndex;
subEnd = columnIndex;
}
}
} }
}
} return s.substr(subStart,maxLen);
}
};
leetcode-【中等题】5. Longest Palindromic Substring的更多相关文章
- 【LeetCode每天一题】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
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- 刷题5. Longest Palindromic Substring
一.题目说明 Longest Palindromic Substring,求字符串中的最长的回文. Difficuty是Medium 二.我的实现 经过前面4个题目,我对边界考虑越来越"完善 ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
随机推荐
- JS-学习-DOM元素尺寸和位置
一,获取元素的css大小 1.通过style内联获取元素的大小 var box = document.getElementById('box'); // 获得元素; box.style. ...
- golang 文件操作
package main import ( "bytes" "fmt" "io" "os" ...
- soanar,jenkins
http://www.sonarqube.org/ https://blogs.msdn.microsoft.com/visualstudioalm/2016/02/18/sonarqube-scan ...
- RestEasy 3.x 系列之三:jsonp
跨域请求解决方法(JSONP, CORS)提到解决跨域可以使用jsonp,RestEasy自带jsonp的拦截器 一.RestEasy的文档如下: If you're using Jackson, R ...
- 10天学会phpWeChat——第三天:从数据库读取数据到视图
在第二天,我们创建了我们的第一个phpWeChat功能模块,但是比较简单.实际生产环境中,我们不可能有如此简单的需求.更多的情况是数据存储在MySql数据库中,我们开发功能模块的作用就是将这些数据从M ...
- [Spring MVC] - JSON
Spring MVC中使用JSON,先必需引用两个包:jackson-core-asl-1.9.13.jar.jackson-mapper-asl-1.9.13.jar 因为需要使用到jquery测试 ...
- bzoj4750: 密码安全
Description 有些人在社交网络中使用过许多的密码,我们通过将各种形式的信息转化为 01 信号,再转化为整数,可以将这个 人在一段时间内使用过的密码视为一个长度为 n 的非负整数序列 A_1, ...
- fMRI数据分析处理原理及方法(转载)
原文地址:http://www.cnblogs.com/minks/p/4889497.html 近年来,血氧水平依赖性磁共振脑功能成像(Blood oxygenation level-depende ...
- 代码安装apk文件
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); in ...
- VS2010 无法计算HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0@VCTargetPath处的属性表达式
VS2010打开.csproj工程文件报错,不能加载,错误信息如下: 无法计算HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\1 ...