[LC] 5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb" Solution 1:
Time: O(N^2)
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s) <= 1:
return s
self.res = ''
for i, char in enumerate(s):
self.helper(i, i, s) # odd
self.helper(i, i + 1, s) # even
return self.res def helper(self, start, end, s, even=False):
while start >= 0 and end < len(s):
if s[start] == s[end]:
if end - start >= len(self.res):
self.res = s[start: end + 1]
start -= 1
end += 1
else:
return
class Solution {
int start = 0;
int maxLen = 0;
public String longestPalindrome(String s) {
int len = s.length();
for (int i = 0; i < len; i++) {
helper(i, i, s);
helper(i, i + 1, s);
}
return s.substring(start, start + maxLen);
}
private void helper(int low, int high, String s) {
while (low >= 0 && high < s.length() && s.charAt(low) == s.charAt(high)) {
low -= 1;
high += 1;
}
if (high - low - 1 > maxLen) {
maxLen = high - low - 1;
start = low + 1;
}
}
}
Solution 2:
Time: O(N^2)
class Solution {
public String longestPalindrome(String s) {
if (s == null || s.length() <= 1) {
return s;
}
boolean[][] isPalin = new boolean[s.length()][s.length()];
int max = 0;
String res = "";
for (int i = 1; i < s.length(); i++) {
// i == j for case of single char
for (int j = 0; j <= i; j++) {
if (s.charAt(i) == s.charAt(j) && (i - j <= 2 || isPalin[i - 1][j + 1])) {
isPalin[i][j] = true;
if (i - j + 1> max) {
max = i - j + 1;
// j is smaller than i
res = s.substring(j, i + 1);
}
}
}
}
return res;
}
}
[LC] 5. Longest Palindromic Substring的更多相关文章
- LN : leetcode 5 Longest Palindromic Substring
lc 5 Longest Palindromic Substring 5 Longest Palindromic Substring Given a string s, find the longes ...
- 5. Longest Palindromic Substring 返回最长的回文子串
[抄题]: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- [LeetCode_5] Longest Palindromic Substring
LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...
随机推荐
- nodejs(6)express学习
1.简单认识express express::一个快速的网站开发框架,封装了原生的http模块,用起来更方便:API更人性化 特点 基于Node.js平台之上,进一步封装了 http 模块,从而提供了 ...
- Maven学习(一)——maven入门
一.下载及安装 1.1 下载maven 3.1.1 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.1.1 ),下载完成后,解压到某个目录(本 ...
- centos 7 内存压测测试--memtester工具
1.下载memteste工具 官方:http://pyropus.ca/software/memtester/ wget http://pyropus.ca/software/memtester/ol ...
- git clone与git pull区别
从远程服务器克隆一个一模一样的版本库到本地,复制的是整个版本库,叫做clone.(clone是将一个库复制到你的本地,是一个本地从无到有的过程)从远程服务器获取到一个branch分支的更新到本地,并更 ...
- winform程序常用图标网站及资源
1.easyicon网站,免费下载 https://www.easyicon.net/ 2.findicons https://findicons.com/ 3.iconarchive http:// ...
- Python笔记_第四篇_高阶编程_高阶函数_2.filter
1. filter函数: 原型:filter(fn,lsd) 参数1为函数 参数2为序列 功能:用于过滤序列,把传入的函数一次作用域序列每个元素,根据返回的是True还是False决定是否保留该元素. ...
- 遇到屏蔽selenium的站点如何突破
访问某团外卖,查看下一页商家信息,正常浏览器可以打开, selenium打开就404, 分析请求参数,生成方法最后定位到 rohr*.js 而且有判断selenium特征 抓耳挠腮搞了半天没把这个j ...
- Ubuntu---Git
本篇文章简单总结了常用 Git 的使用 前言 设置用户信息 1, Git 是分布式的 SSH 代码管理工具,远程的代码管理是基于 SSH 的,所以要使用远程的 Git 则需要 SSH 的配置. ste ...
- JavaSE--RMI初识
转自:http://blog.csdn.net/guyuealian/article/details/51992182 一.Java RMI机制: 远程方法调用RMI(Remote Me ...
- OutOfMemoryError异常
1.Java堆溢出 Java堆用于存储对象实例,只要不断地创建对象,并且保证GC Roots到对象之间有可达路径来避免垃圾回收机制清除这些对象,就会在对象数量达到最大堆的容量限制后产生内存溢出异常. ...