leetcode个人题解——#5 Container with most water
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的更多相关文章
- leetcode个人题解——#11 Container with most water
class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- Leetcode题解之Container With Most Water
1.题目描述 2.题目分析 首先,这个题可以使用暴力解法,时间复杂度是O(n^2),这个显然是最容易的做法,但是效率不够高,题目提供了一种解法,使用两个指针,一个从头向尾部,另外一个从尾部向头部,每一 ...
- LeetCode 笔记系列二 Container With Most Water
题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- leetcode第11题--Container With Most Water
Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- 【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). ...
- LeetCode(11) Container With Most Water
题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...
- LeetCode(11)Container With Most Water
题目如下: 题目的意思是求容器能装的最大的水量,当时我按梯形的面积来算,一直不对,后来才发现要按矩形的面积来算 Python代码如下: def maxArea(self, height): " ...
随机推荐
- oracle系列(二)用户管理
SQL> conn /as sysdbaConnected to Oracle Database 11g Express Edition Release 11.2.0.2.0 Connected ...
- QLViewController在iOS7下的自定义
原文来自:QLViewController在iOS7下的自定义 原先的项目使用了quicklook framework,用于在iPhone上浏览各类文件,除了txt文本会有乱码的问题,其他文件的显示都 ...
- iOS universallinks唤醒app
从iOS9之后,苹果就推出了这个功能,用来唤醒外部app.这个功能在那些电商app上使用尤其广泛,当你打开对应的h5网页后,上面跳出一个是否跳转app的按钮. 现在iOS11已经基本覆盖,iOS12也 ...
- Ubuntu16 安装Anaconda3+tensorflow cpu版
打开火狐浏览器,下载anaconda安装包,网址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=D 下载完成,到Do ...
- hql返回数值
public int getCountUser() throws ParseException { Session hSession = sessionFactory.getCurrentSessio ...
- thinkphp5实现定位功能
一.所需资源链接:百度网盘.主要包含一个ip地址库和一个ip类文件. 二.下载好后,在extend目录下面创建一个location的目录,将下载的文件解压到该目录.给类文件增加一个命名空间,便于我们使 ...
- Mongodb从库配置
1. 先以master方式启动mongodb 2. 导入主库的数据文件:/data/mongodb-3.0.12/bin/mongorestore /data/tmp/mongodbbak/ 3. 关 ...
- php 遍历一个文件夹下的所有文件和子文件
php 遍历一个文件夹下的所有文件和子文件 <?php /** * 将读取到的目录以数组的形式展现出来 * @return array * opendir() 函数打开一个目录句柄,可由 clo ...
- [Doctrine Migrations] 数据库迁移组件的深入解析二:自定义集成
自定义命令脚本 目录结构 目前的项目结构是这样的(参照代码库): 其中,db/migrations文件夹是迁移类文件夹,config/db.php是我们项目原有的db配置,migrations.php ...
- python+selenium实现自动抢票
使用说明 程序运行开始,需要输入出发地,目的地,出发时间,乘客信息,车次:乘客信息和车次可以输入多个 刚刚开始学习爬虫,selenium仅仅是解放了双手,运行效率不是很高: 程序运行时会打开chrom ...