[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 ...
随机推荐
- leetcode算法题121-123 --78 --python版本
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 实例输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能 ...
- F - Moving Points树状数组
题:https://codeforces.com/contest/1311/problem/F 题意:给定x轴上的点以及他们的速度v,只在x轴上运动,求最小的dis之和,注意,这里的时间是可随意的,比 ...
- MySQL--索引和外键
来自:http://www.jb51.net/article/32149.htm 1.添加PRIMARY KEY(主键索引) ALTER TABLE `table_name` ADD PRIMARY ...
- EditText制作简单的登录界面
EditText与之前的TextView和Button的用法大体相同,用法案例如下: activity_edit_text.xml: <?xml version="1.0" ...
- Django2.0——请求与响应(下)
上篇讲完了请求,这篇接着讲下响应,django响应类型大致有以下几种 HttpResponse:返回简单的字符串 render:渲染模板 redirect:重定向 JsonResponse:返回jso ...
- symmetry methods for differential equations,exercise 1.4
tex文档: \documentclass[a4paper, 12pt]{article} % Font size (can be 10pt, 11pt or 12pt) and paper size ...
- luogu P3835 【模板】可持久化平衡树
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> usin ...
- linux的centos设置静态网络
这个是该自己的网络排至,具体的分析,自己以后再研究 http://www.centoscn.com/CentOS/config/2015/0227/4753.html
- 递归与树的写法-多种支付的设计-支付的接通-celery订单的回退实现
递归与树的写法 data: data=[ {"cat_id":1,"name":"北京","parent_id":0}, ...
- 理解python的可变参数
以 str.format(*args,**kwargs) 为例. "type1:{},{},{},{}_type2:{a},{b},{c},{d}".format('a',2,*[ ...