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.

玩了两天dota2,罪过罪过,还是应该老老实实刷题啊。

题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以及"assa",即奇数以及偶数回文,相当于用了二重循环,代码如下:

 class Solution {
public:
string longestPalindrome(string s) {
if (!s.size()) return "";
string ret;
string tmp;
for (int i = ; i < s.size(); ++i){
tmp = findPal(s, i - , i + );
if (tmp.size() > ret.size())
ret = tmp;
tmp = findPal(s, i , i + );
if (tmp.size() > ret.size())
ret = tmp;
}
return ret;
} string findPal(string & s, int left, int right)
{
if (left < )
return s.substr(left + , );
if (right >= s.size())
return s.substr(right - , ); while (left >= && right < s.size()){
if (s[left] != s[right])
break;
left--;
right++;
}
left++;
return s.substr(left, right - left);
}
};

最新文章

  1. C#如何在事件中获得GridView里面TextBox的值
  2. JSON (仅限本地)
  3. IOS UItableView 滚动到底 触发事件
  4. Extension Methods
  5. 改变seekbar的游标图片大小
  6. target,currentTarget,delegateTarget,srcElement
  7. 【Tools】Pycharm2017 windows安装与修改中文界面教程
  8. echarts中的区域缩放组件dataZoom,主动触发选区缩放点击事件
  9. 辨析字节序(Endianness)
  10. Python3.7 Scrapy crawl 运行出错解决方法
  11. Emacs 中使用 shell
  12. maven编译项目报错,提示找不到符号或程序包XXX不存在
  13. SEO 图片用IMG插入好还是用Background定义好?
  14. 各种 mv power cell
  15. jQuery最简单的表单提交方式
  16. eclipse在线安装maven插件
  17. List自定义排序
  18. 史上最详细的Hadoop环境搭建(转)
  19. React-Navigation web前端架构
  20. leetcode-对称二叉树

热门文章

  1. 目标检测之R-FCN
  2. django settings相关配置
  3. go——工程结构
  4. bootstrap基本使用
  5. Php DOMDocument 中的 formatOutput
  6. Codeforces Round #395 (Div. 2) C. Timofey and a tree
  7. DateTable To Json
  8. jQuery动态网址标签
  9. vue切换路由模式{hash/history}
  10. Go语言学习之数据类型以及类型转换(The way to go)