[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 ...
随机推荐
- {转} 深入理解 HTTP Session
session在web开发中是一个非常重要的概念,这个概念很抽象,很难定义,也是最让人迷惑的一个名词,也是最多被滥用的名字之一,在不同的场合,session一次的含义也很不相同.这里只探讨HTTP S ...
- chr()//ord() //进制转换函数//eval()//文件函数//split()
1.chr() 函数 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 用法:chr(i) i可以是10进制也可以是16进制的形式的数字. 2.or ...
- P(Y=y|x;θ)表示什么意思
https://blog.csdn.net/jh1137921986/article/details/88999539 在机器学习中,特别是学习到关于概率/似然估计方面的内容,经常看到类似P(Y=y| ...
- caffe fastercbnnahdemo
https://download.csdn.net/download/zefan7564/10148990 https://blog.csdn.net/qq_37124237/article/deta ...
- Linux ES集群服务配置说明
说明: ES官网不建议在root用户使用Elastic Server,因此ES集群配置均使用普通账户操作,新建账户 elastic. Linux版本为CentOS 7.3,ES版本为5.5.0. 一. ...
- SVN提交时忽略不必提交的文件夹和文件,如node_modules
空白处右键>选中TortoiseSVN>设置(settings)>常规设置(General)>Subversion>编辑(edit)>在弹出的config文件中找g ...
- java实现图片和pdf添加铺满文字水印
依赖jar包 <!-- pdf start --> <dependency> <groupId>com.itextpdf</groupId> <a ...
- 2019收藏盘点(编程语言/AI/面试/实用工具)
2020.1.5更新 我看过的后面会加上评价 编程学习 java开源项目汇总: https://github.com/Snailclimb/awesome-java 大数据学习入门: https:// ...
- ORM表之间高级设计
ORM表之间高级设计 一.表的继承 # db_test1 # 一.基表 # Model类的内部配置Meta类要设置abstract=True, # 这样的Model类就是用来作为基表 # 多表:Boo ...
- Microsoft.Office.Inter.Excel.dll在調用時可能會出現如下錯誤
Microsoft.Office.Inter.Excel.dll在調用時可能會出現如下錯誤,具體解決方案如下: 1. 錯誤資訊:檢索 COM 類工廠中 CLSID 為{00024500-0000-00 ...