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的更多相关文章

  1. LN : leetcode 5 Longest Palindromic Substring

    lc 5 Longest Palindromic Substring 5 Longest Palindromic Substring Given a string s, find the longes ...

  2. 5. Longest Palindromic Substring 返回最长的回文子串

    [抄题]: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  5. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  7. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  8. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  9. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

随机推荐

  1. @Autowired注解与@Resource注解的区别(详细)

    相信对现在Java码农来说,@Autowired跟@Resource并不陌生,二者都可以自动注入,但是两者的区别很多时候并没有被注意到. 一.注解的出处 @Autowired是Spring提供的注解, ...

  2. python课后练习当前目录下有一个文件名为score3.txt的文本文件, 存放着某班学生的学号和其两门专业课的成绩。

    题目: 当前目录下有一个文件名为score3.txt的文本文件, 存放着某班学生的学号和其两门专业课的成绩.分 别用函数实现以下功能: (1) 定义函数function1,计算每个学生的平均分(取 整 ...

  3. 201712-1 最小差值 Java

    思路: 也可以不排序,最后用abs就行 import java.util.Arrays; import java.util.Scanner; public class Main { public st ...

  4. JAVA课程设计——俄罗斯方块

    0.负责模块为可视化界面,技术栈为 (1)异常处理 (2)多线程 (3)文件存储 (4)Java swing 1.登陆界面 我的代码 import java.awt.Color; import jav ...

  5. Java--类初始化

    package httpclient.demo; public class StaticTest { public static void main(String[] args) { staticFu ...

  6. List中常用的linq操作

    [Serializable] public class Product { public Product() { } public Product(string id,string pname,int ...

  7. Facebook的Libra “区块链”到底是如何运作的?

    本文深入研究了"关于Facebook Libra coin (以及更多)平台协议"的26页技术文档,并对其内容进行了分解说明.同时,我们对这53位作者表示衷心的钦佩! 以下为具体分 ...

  8. git submodule update --init 和 --remote的区别

    git 的submodule 工具方便第三方库的管理,比如gitlab 上的各种开源工具,spdlog等 在项目目录下创建.gitmodule 里可以添加第三方库,然后在更新第三方库时,有两个选项 g ...

  9. flask框架-大结局

    flask-script 用于实现类似于django中 python3 manage.py runserver ...类似的命令. 安装 pip3 install flask-script 使用: f ...

  10. aop 实现原理

    aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...