leetcode——5——Longest Palindromic Substring
2016-04-13 16:51
155 查看
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.
S is 1000, and there exists one unique longest palindromic substring.
class Solution { public: string str_helper(string s, int left, int right) { int n = s.length(); while(left>=0 && right<=n-1 && s[left]==s[right]){ left--; right++; } return s.substr(left+1, right-left-1); } string longestPalindrome(string s) { if(s.length() <= 1) return s; string longest = s.substr(0,1); for (int i=0; i<s.length(); i++){ string tmp = str_helper(s, i, i); if (tmp.length() > longest.length()) { longest = tmp; } tmp = str_helper(s, i, i+1); if (tmp.length() > longest.length()) { longest = tmp; } } return longest; } };
相关文章推荐
- JAVA运行脚本语言(Scripting)
- char *p[]={"Shanghai","Beijing","Honkong"};则结果为j字符的表达式是
- HM编码器代码阅读(7)——整个编码流程以及相关的函数
- Delphi动态调用C++写的DLL
- 获取被启动的Activity传回的参数:
- mapreduce原理分析
- HDFS存储系统
- 倒计时四天——2727:仙岛求药
- lintcode:买卖股票的最佳时机 III
- DeviceInfo-设备信息
- Libnids详解
- Android海量字体设置
- App上架流程
- 未能理解 掌握的
- 软件工程课堂测试07(结对开发)
- 聚合数据API用法简介
- 1.设备驱动基础
- 3. 标准库类型
- mysql 常用函数总计
- Direct2D Intro - Clip a Bitmap Source