LeetCode:5. Longest Palindromic Substring(Medium)
原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/
1. 题目要求:找出字符串中的最大回文子串
2. 注意:要考虑回文子串中的字符个数是奇数还是偶数!!!
例如,“aabaa”是一个奇数个字符的回文字符串,他的中心只有一个字符“b”。
“aabbaa”是一个偶数个字符的回文字符串,他的中心却有两个相同字符“bb”
3. 思路:暴力解决,以每个字符为中心,对该字符的两边进行匹配,找出最长的回文子串1;再以两个相邻字符为中心,进行同样操作,找出最长回文子串2。然后对两个子串进行比较,取最长者。
class Solution {
public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}
private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
}
}
LeetCode:5. Longest Palindromic Substring(Medium)的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- 5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
- LeetCode:12. Integer to Roman(Medium)
1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...
- Leetcode 之Longest Palindromic Substring(30)
很经典的一道题,最长回文子串,有多种方法. 首先介绍的一种方法是从中间向两边展开.注意区分aba和abba型的回文串:如果当前最长的子串已经当于两边中最长的子串了,则无需再去判断. //从中间向两边展 ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- LeetCode OJ:Longest Palindromic Substring(最长的回文字串)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- A potentially dangerous Request.Form value was detected from the client的解决办法
网上找了这么多,这条最靠谱,记录下来,以备后用 <httpRuntime requestValidationMode="2.0"/> <pages validat ...
- bootstrap table 怎么实现前两列固定冻结?
$("#Table").bootstrapTable('destroy').bootstrapTable({ pagination: true,//分页 minimumCountC ...
- sudo: Sorry, you must have a tty to run
The requiretty option in sudoers file The requiretty if set in sudo config file sudoers, sudo will o ...
- java反射 反射构造函数 报 wrong number of arguments 错误
package com; import java.lang.reflect.Constructor; public class Person { public Person() { } public ...
- HttpHandler使用Session
继承自IHttpHandler的类要实现两个接口:ProcessRequest和IsReusable但还不能使用Session,要使用Session需要下面的步骤处理: 1.先引用System.Web ...
- PHP中级面试经历
资源来自网络:http://www.2cto.com/kf/201304/204749.html 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) 2.echo(),p ...
- SqlSugar批量添加修改问题
直接InsertRange空集合会报错,如果我们是同时执行多个添加或修改,不要共用一个上下文,最好是在方法里面声明上下文进行区分,不然容易报错 //如果同时执行多个添加,更新 操作不要共用一个上下文, ...
- 任务学习-ucos
1.任务(task)也称作一个线程: 2.一个任务有5种状态:休眠,就绪,运行,挂起,被中断 休眠:任务驻留在程序空间中,还没有交给ucos管理,把任务交给ucos 是通过调用OSTaskCreate ...
- Java基础——数组复习
数组是一个变量,存储相同数据类型的一组数据 声明一个变量就是在内存空间划出一块合适的空间 声明一个数组就是在内存空间划出一串连续的空间 数组长度固定不变,避免数组越界 数组是静态分配内存空间的,所 ...
- SpringBoot非官方教程 | 终章:文章汇总
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-all/ 本文出自方志朋的博客 SpringBo ...