LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
2024-08-30 00:07:51
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);
}
};
最新文章
- C#如何在事件中获得GridView里面TextBox的值
- JSON (仅限本地)
- IOS UItableView 滚动到底 触发事件
- Extension Methods
- 改变seekbar的游标图片大小
- target,currentTarget,delegateTarget,srcElement
- 【Tools】Pycharm2017 windows安装与修改中文界面教程
- echarts中的区域缩放组件dataZoom,主动触发选区缩放点击事件
- 辨析字节序(Endianness)
- Python3.7 Scrapy crawl 运行出错解决方法
- Emacs 中使用 shell
- maven编译项目报错,提示找不到符号或程序包XXX不存在
- SEO 图片用IMG插入好还是用Background定义好?
- 各种 mv power cell
- jQuery最简单的表单提交方式
- eclipse在线安装maven插件
- List自定义排序
- 史上最详细的Hadoop环境搭建(转)
- React-Navigation web前端架构
- leetcode-对称二叉树