[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 ...
随机推荐
- ArrayList扩容原理分析
1:代码解读和分析 1.1:构造方法分析 1: public ArrayList(int initialCapacity) { ) { this.elementData = new Object[in ...
- [极客大挑战 2019]EasySQL
万能密码直接登陆得到flag admin' or 1=1 #
- python pandas写入excel文件
pandas读取.写入csv数据非常方便,但是有时希望通过excel画个简单的图表看一下数据质量.变化趋势并保存,这时候csv格式的数据就略显不便,因此尝试直接将数据写入excel文件. pandas ...
- [Java-基础]反射_Class对象_动态操作
动态性 动态语言 在程序运行时,可以改变程序结构或变量类型,典型的语言: Python,ruby,javascript 如: function test(){ var s = "var a= ...
- Java - 记录String中intern()方法的学习与理解
intern()方法:把堆中的引用丢入常量池中,然后返回这个引用.当常量池中已经存在这个引用,就直接返回这个引用.(jdk1.8) 由于jdk1.7中将字符串常量池改为存放在堆中,因此intern() ...
- PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]
题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...
- CocoaPods-Alcatraz插件
Alcatraz:Xcode的插件管理工具,可通过它添加CocoaPods插件 下载地址:https://github.com/alcatraz/Alcatraz 建议: 不提倡通过终端命令下载Alc ...
- curl命令简介
curl 文件传输工具 参数: -c --cokie-jar: 将cookie写入到文件 -b --cokie: 从文件中读取cookie -C --continue-at: 断点续传 -d --da ...
- JavaWeb中的高级知识总结
知识结构图 文件下载 默认情况下,如果浏览器可以处理Content-Type响应头中指定数据类型,浏览器就会直接处理,比如显示出HTML页面(text/html),或者显示出照片(image/png) ...
- JVM内存结构图表展示
1.理解的JVM内存结构 2.对于垃圾回收问题 垃圾的回收只在堆和永久区(方法区)中,因为对于线程而言,私有存储空间如栈.本地方法区.程序计数器等,会随着方法的加载完成而直接释放空间,因此不需要进行 ...