[LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring
class Solution {
public:
//动态规划算法
string longestPalindrome(string s) {
int n = s.length();
int longestBegin = 0;
int maxLen = 1;
bool table[1000][1000] = {false};
for (int i = 0; i < n; i++) {
table[i][i] = true;
}//对角设为1
for (int i = 0; i < n-1; i++) {
if (s[i] == s[i+1]) {
table[i][i+1] = true;
start = i;
maxLen = 2;
}
}//检查是否存在"aa"这样的
for (int len = 3; len <= n; len++) {
for (int i = 0; i < n-len+1; i++) {
int j = i+len-1;
if (s[i] == s[j] && table[i+1][j-1]) {
table[i][j] = true;
start = i;
maxLen = len;
}
}
}
return s.substr(start, maxLen);
}
};
[LeetCode_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 ...
- 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 le ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- ncurses库的一些函数
为了实现一个简单的聊天程序,如果使用普通的输入输出函数,会很凌乱.so,便想着能不能用下 ncurses这个字符图形库 总结一下,就是这样. 使用ncurses时,先需要初始化窗口,程序结束时,主动调 ...
- 随手记一次用C#正则表达式获取下拉菜单html标签<select>以及相关属性值
随手记一次用C#正则表达式获取下拉菜单html标签<select>以及相关属性值 1:有如下html: .................. <select id="aaa ...
- Java反射使用技巧
1. 通过setAccessible关闭安全检查,关闭的目的不是因为访问的field/method是私有的,而且因为关闭后访问公有方法也不会再有安全检查. SomeObject someObject ...
- 终端启动apache,mysql服务;登录mysql服务器
sudo apachectl start sudo mysql.server start sudo apachectl help 查看帮助 mysql -hlocalhost -uroot -p ma ...
- Android pop3与imap方式接收邮件(javamail)
需要下载3个jar包:mail.jar/ activation.jar/ additionnal.jar 1.pop3 /** * 以pop3方式读取邮件,此方法不能读取邮件是否为已读,已 ...
- java环境变量的设置
java安装好后需要配置一下环境变量,配置方法如下: 1.在系统变量里添加两条记录: 1)变量名:JAVA_HOME,变量值为java安装路径,如:C:\Program Files\Java\jdk1 ...
- 安装redis
第一步 下载 第二步 解压 .tar.gz 第三步 make cd redis- make 第四步 启动试一下 src/redis-server 好了 :C Jan ::13.501 # Warni ...
- how-to-install-siege-on-centos-7
https://www.joedog.org/siege-home/ https://roastahost.com/how-to-install-siege-on-centos-7/ (Works!) ...
- lua协程一则报错解决“attempt to yield across metamethod/C-call boundary”
问题 attempt to yield across metamethod/C-call boundary 需求跟如下帖子中描述一致: http://bbs.chinaunix.net/forum.p ...
- Android四大核心组件之BroadCastReceiver
实验内容 实现BroadCast发送和接受 通过BroadCast传递信息 动态注册和注销BroadCast 实验要求 实现BroadCast发送和接受 通过BroadCast传递信息 动态注册和注销 ...