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): " ...
随机推荐
- 《Linux 学习》01---redis安装, 并使用Redis Desktop Manager 连接
一.环境简介: linux 系统:centos 7.X 二.安装大纲: 1.下载安装包 2.安装 3.统一管理redis 配置文件 4.编辑redis配置文件,设置常用的功能 5.(1)命令启动,连接 ...
- ios之runtime
简介 OC是从C语言发展过来的, 之所以能变为动态语言是因为runtime机制, runtime就是OC在运行时的一些机制: OC的runtime最重要的是消息机制. 在编译阶段,OC可以调用任意函数 ...
- Deepin深度Linux系统安装记录
测试设备:小米游戏本,最新版15.6进入安装后发现黑屏,所以使用15.5安装 Deepin 15.5 官方介绍页 官方下载 百度云下载 下载后得到文件夹15.5 Release 将里面的ISO镜像文件 ...
- app后端api设计【转】
博客:https://blog.csdn.net/newjueqi/article/details/44037011 app和后端的交互,一般都是通过后端提供的api实现.api的设计,估计很多刚进入 ...
- js 校验身份证号
根据地区编码.身份证格式.18位身份证需要验证最后一位校验位 //校验身份证 function IdentityCodeValid(code) { var city = { 11: "北京& ...
- 大数据学习--day06(Eclipse、数组)
Eclipse.数组 Eclipse 的基本设置 调节控制条字体大小. Window -> Preferences -> General -> Appearance -> ...
- 2.3 摄像头驱动_vivi驱动程序分析
学习目标:熟悉vivi的调用过程,分析vivi程序源码的ioctl函数: 一.vivi虚拟视频驱动测试方法 当我们接上usb摄像头设备时,系统会自动给我们安装对应的usb设备驱动程序.如果下次直接测试 ...
- go内建容器-Map
1.基础定义 golang中的map如同它的函数一样"纯粹",map就是用来存储键值对的容器,别管什么哈希不哈希的(底层已实现),用就行 //创建一个map m := map[st ...
- leetcode add_binary 采坑记
尽管add_binary在leetcode中是一个简单难度,但是踩了不少坑,记录一下 描述: 给两个字符串形式的二进制数,要求求和并输出字符串形式的结果,其中a和b均不为空字符串 样例: a=“101 ...
- 20145202马超《网络对抗》Exp9*_* Web安全基础实践
本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 感觉上次做的也是sql注入,就是故意的非法输入(输入的是一些指令)让 ...