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): " ...
随机推荐
- sql中UNION和UNION ALL的区别
写sql时我们经常会遇到需要把从多张表查询的集果集进行合并.这时就用到了union.使用union或union all 时一定要保证查询的列的一致性 .不然sql会报错.字段不一致的话可以用单引号来占 ...
- Codeforces Round #487 (Div. 2)
A. A Blend of Springtime(暴力/模拟) 题目大意 给出$n$个花,每个点都有自己的颜色,问是否存在连续大于等于三个花颜色均不相同 sol 直接模拟判断即可 #include&l ...
- Eclipse报The default workspace'xxxxx' is in use or cannot be created Pl
原因:出现这种情况一般是workspace的配置文件中出现了.lock文件(workspace/.metadata/.lock),锁定了workspace.把.lock文件删除即可.如果该文件不能删除 ...
- vue 项目 切换手机端和pc端。同一个项目,配置不同的路由
1, 首先判断设备:在main.js里面写 // vue原型挂载 - 是否PC端 if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator. ...
- ziplist之详细分析
压缩列表ziplist ziplist是一种连续,无序的数据结构.压缩列表是 Redis 为了节约内存而开发的, 由一系列特殊编码的连续内存块组成的顺序型(sequential)数据结构. 组成 属性 ...
- MapReduce清洗日志数据统计PV量
package mapreduce.webpv; import java.io.IOException; import org.apache.commons.lang.StringUtils; imp ...
- java语言描述 用递归打印字符串
public class Test{ static private int n; public static void main(String[] args) { Test.n=76234; if(n ...
- auto、static、extern
- TensorFlow之tf.nn.dropout():防止模型训练过程中的过拟合问题
一:适用范围: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层 二:原理: dropout就是在不同的训练过程中随机扔掉一部分神经元.也就是让 ...
- FPGA烧完程序之后,检测不到网口的
原因:未给phy芯片添加复位 解决方法:在程序顶部添加一个输出信号output e_reset,使其值一直为高. output e_reset, 'b1;