class Solution {
public:
string longestPalindrome(string s) {
int length = s.length();
if (length == ) return s;
int len = ;
int begin = ;
int **pS = new int *[length];//palindromic state
for (int i = ;i < length; i++)
pS[i] = new int[length+];
for( int i = ; i < length;i++)
{
pS[i][] = ;
pS[i][] = ;
}
for(int i = ; i <= length; i++)
{
for (int j = ;j < length-i+; j++)
{
if (s[j] == s[j+i-] && pS[j+][i-] == ) {
len = i;
pS[j][i] = ;
begin = j;
}
}
}
string ans = "";
for (int i = begin; i < begin + len; i++)
ans += s[i];
return ans;
delete [] pS;
}
};

对暴力解法做了一点优化,中间每一步结果存了下来,存放在pS[i][[j]中,这个数组意义是从第i个字符开始后长度为j的字符串是否为回文。

附上大佬的解法:

string longestPalindrome(string s) {
if (s.empty()) return "";
if (s.size() == ) return s;
int min_start = , max_len = ;
for (int i = ; i < s.size();) {
if (s.size() - i <= max_len / ) break;
int j = i, k = i;
while (k < s.size()- && s[k+] == s[k]) ++k; // Skip duplicate characters.
i = k+;
while (k < s.size()- && j > && s[k + ] == s[j - ]) { ++k; --j; } // Expand.
int new_len = k - j + ;
if (new_len > max_len) { min_start = j; max_len = new_len; }
}
return s.substr(min_start, max_len);
}

leetcode个人题解——#5 Container with most water的更多相关文章

  1. leetcode个人题解——#11 Container with most water

    class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...

  2. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  4. Leetcode题解之Container With Most Water

    1.题目描述 2.题目分析 首先,这个题可以使用暴力解法,时间复杂度是O(n^2),这个显然是最容易的做法,但是效率不够高,题目提供了一种解法,使用两个指针,一个从头向尾部,另外一个从尾部向头部,每一 ...

  5. LeetCode 笔记系列二 Container With Most Water

    题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  6. leetcode第11题--Container With Most Water

    Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  7. 【LeetCode two_pointer】11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  8. LeetCode(11) Container With Most Water

    题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...

  9. LeetCode(11)Container With Most Water

    题目如下: 题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算 Python代码如下: def maxArea(self, height): " ...

随机推荐

  1. 【Django笔记二】Django2.0配置模板和静态文件

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 二.创建模板 1.在my_project文件夹下新建文件夹templates用于存放模板文 ...

  2. 811. Subdomain Visit Count (5月23日)

    解答 class Solution { public: vector<string> subdomainVisits(vector<string>& cpdomains ...

  3. java8的新特性,Collections.sort(排序的List集合)的使用,对list封装Map里面的某个值进行排序

    --------------------------对简单list的排序---------------------------------- List<Integer> list = ne ...

  4. Cobbler实现自动化安装(上)--原理篇

    了解Cobbler之前,我们需要先对PXE及KickStart有一定的认识. PXE PXE(Pre-bootExecution Environment),预启动执行环境,通过网络接口启动计算机,支持 ...

  5. WebService 学习笔记(一、概念及定义)

    定义 WebService是一种服务导向架构(SOA service-oriented architecture)的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作. Web ...

  6. HTML学习日记之元信息meta标记

    所谓meta标记就是用来描述一个HTML网页文档的属性,也称为元信息,这些信息并不会显示在浏览器的页面中,例如作者.日期和时间.网页描述.页面刷新等. 基本语法: <meta name = &q ...

  7. drawImage画本地资源,在真机无法显示

    把图片的路径改成本地的绝对路径

  8. Product Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 产品 ...

  9. 论文翻译第二弹--用python(或Markdown)对论文复制文本进行处理

    图中这种论文你想进行文本复制放入翻译软件进行翻译时,会发现是这种形式: 句子之间是断开的,这时普遍的方法,也是我之前一直用的方法就是打开一个文档编辑器,复制上去后一行行地继续调整.昨天不想这样了,就打 ...

  10. mysql库地址

    https://dev.mysql.com/downloads/connector/