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" 题意就是给一个字符串,找到子串中最大的对称字符串。
我的想法就是分为两种情况,奇数情况和偶数,从其中一个字符或者两个字符往前后两个方向进行扫描。
public String longestPalindrome(String s) {
String longestPalindrome = "";
for (int i = 0; i < s.length(); i++) {
//奇数情况
int pre = i;
int next = i;
String s1 = getLongestPalindromeByIndex(pre, next, s);
//偶数情况
next++;
String s2 = getLongestPalindromeByIndex(pre, next, s);
s1 = s1.length() > s2.length() ? s1 : s2;
longestPalindrome = longestPalindrome.length() >= s1.length() ? longestPalindrome : s1;
}
return longestPalindrome;
} public static String getLongestPalindromeByIndex(int pre, int next, String s) {
while (pre >= 0 && next < s.length() && s.charAt(pre) == s.charAt(next)) {
pre--;
next++;
}
//当退出循环时,说明当前pre和next不相等,从pre+1截取到next-1
return s.substring(pre + 1, next);
}

  这个代码有一个问题,就是产生了很多无用的字符串,其实可以将返回值变成start和end也就是开始索引和结束索引,

但是我感觉这样写可读性好一点。

LeetCode第五题:Longest Palindromic Substring的更多相关文章

  1. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  2. leetcode第五题--Longest Palindromic Substring

    Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...

  3. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  4. leetcode--5 Longest Palindromic Substring

    1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...

  5. LeetCode(5)Longest Palindromic Substring

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

  6. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

    [Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...

  7. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

  8. 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)

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

  9. (python)leetcode刷题笔记05 Longest Palindromic Substring

    5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

随机推荐

  1. Shell编程之case条件

    一.case条件语句 1.语法 case "变量" in 值 1) 指令 1... ;; 值 2) 指令 2... ;; *) 指令 3... esac case条件语句的执行流程 ...

  2. 我的python开发目录模块连接

    一.python语言 二.HTML 三.css 四.javascript 五.DOM 六.jquery 七.AJAX 八.WEB前端插件 九.自定义WEB框架 十.WEB框架之tornado 十一.M ...

  3. 服务器Windows 2008 R2 安装SQL 2008 R2

    在站点下载 SQL 2008 R2 在安装数据库之前首先安装IIS和.NET 3.5 解压  找到运行程序 (这里需要修改路径,数据库一般不要安装在系统盘) (选择任何一个都可以,这里选择system ...

  4. lua闭包浅析及项目应用

    lua函数与闭包: 原文地址:http://www.doc88.com/p-6681238341344.html 近日查阅关于lua的一些资料,找到了我能理解的关于lua函数与闭包的解析,我觉得这个程 ...

  5. django学习笔记整理(1)django的MTV模式

    django作为一个python的网络编程的框架,自然有着其规律可循.通过对django的了解,也明白了一些网络编程的知识.最近这近一个月,在网上查了许多文字资料,也看了别人的视频之类的资料,也算是对 ...

  6. Java之File文件类

    package IoDemo; import java.io.File; import java.io.FileFilter; import java.io.IOException; import j ...

  7. Android Studio2.3中简单配置,释放C盘空间

    重新安装了一下android studio,由于占用了太多的C盘空间.记录一下,在网上收集到的studio中两个主要占用C盘空间的文件,我们将它移除C盘. 原博地址: http://blog.csdn ...

  8. eclipse build path 以及 clean(转)

    1.设置"source folder"与"output folder". source folder:存放.Java源文件的根目录:output folder: ...

  9. Java执行过程

    Java的运行原理 在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能 ...

  10. WinDBG__独立安装文件

    debugging tools for windows 1. 来自于网页:http://rxwen.blogspot.hk/2010/04/standalone-windbg-v6120002633. ...