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. nf_conntrack之解决方案

    1.现象 在/var/log/message中出现以下信息 Dec 8 11:22:29 product08 kernel: nf_conntrack: table full, dropping pa ...

  2. hadoop实战项目:查找相同字母组成的字谜

    前面我们学习了MapReduce编程思想和编程示例,那么本节课程同学们一起操练操练,动手完成下面的项目. 项目需求 一本英文书籍包含成千上万个单词或者短语,现在我们需要在大量的单词中,找出相同字母组成 ...

  3. OC_内存管理

      引言:      1.OC中的对象都是分配在堆中的                声明对象的格式:                     Person *person = [Person new ...

  4. Android系统开发--灯光系统之电池灯的流程分析

    Android系统开发--Android灯光系统之电池灯的流程分析 前期系统准备 运行初始化,创建系统服务 创建电池服务,获得电池灯;创建监听者监听上报电池事件: mSystemServiceMana ...

  5. Linux 内核是如何构建

    https://github.com/MintCN/linux-insides-zh 介绍 我不会告诉你怎么在自己的电脑上去构建.安装一个定制化的 Linux 内核,这样的资料太多了,它们会对你有帮助 ...

  6. myeclipse 10 j安装了JDK1.7,java编译器无法选择到1.7的问题

    java程序编写,在eclipse中会自动编译,编译的版本在preferrence-->java-->compiler选择具体版本,这时你写程序时自动编译用的jdk就是这个版本的jdk,这 ...

  7. Fatal error: cannot create 'R_TempDir'

    [user@mgmt dir]$ R Fatal error: cannot create 'R_TempDir' [user@mgmt dir]$ ll -ad /tmp drwxrwxrwt. 2 ...

  8. sql server 2005 Express 下载

    简体中文版: SQL Server 2005 Express Edition 简体中文版 链接页面: http://www.microsoft.com/downloads/details.aspx?d ...

  9. JConsole操作手册

    一篇Sun项目主页上介绍JConsole使用的文章,前段时间性能测试的时候大概翻译了一下以便学习,今天整理一下发上来,有些地方也不知道怎么翻,就保留了原文,可能还好理解点,呵呵,水平有限,翻的不好,大 ...

  10. C#将字符转换成utf8编码 GB321编码转换

    public static string get_uft8(string unicodeString) { UTF8Encoding utf8 = new UTF8Encoding(); Byte[] ...