【leetcode】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, and there exists one unique longest palindromic substring.
解题分析:
之前百度实习生面试也被问到这个问题。这个题比较简单。就是在分别考虑对称字符字串为奇数和偶数的情况,在不超过字符串范围的情况下向两边扩展,找到最大值,并记录下最大子串的位置,最后返回即可。
具体代码:
public class Solution {
public static String longestPalindrome(String s) {
if(s==null)
return null;
if(s.length()==0)
return "";
if(s.length()==1)
return s;
if(s.length()==2){
if(s.charAt(0)!=s.charAt(1)){
return s.substring(1);
}
else
return s;
}
int max=1;
int start=0;
int end=0;
char[] array=s.toCharArray();
//对称字符字串长度为数的情况
for(int i=1;i<s.length()-1;i++){
int from=i;
int to=i;
// 要保证不超过字符数组边界的情况下,待扩展的两字符相等
while(from>=0&&to<s.length()&&array[from]==array[to]){
from--;
to++;
}
int len=to-from-1;
if(len>max){
start=from+1;
end=to-1;
max=len;
}
}
//对称字符字串长度为偶数的情况
for(int i=0;i<s.length()-1;i++){
if(array[i]!=array[i+1])
continue;
int from=i;
int to=i+1;
while(from>=0&&to<s.length()&&array[from]==array[to]){
from--;
to++;
}
int len=to-from-1;
if(len>max){
start=from+1;
end=to-1;
max=len;
}
}
return s.substring(start,end+1);
}
}
【leetcode】5. Longest Palindromic Substring的更多相关文章
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【LeetCode】005. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 【一天一道LeetCode】#5 Longest Palindromic Substring
一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- 【leetcode】516. Longest Palindromic Subsequence
题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- [AngularJS] Test an Angular Component with $componentController
Traditionally you had to create DOM elements to test a directive but by shifting our focus to compon ...
- MHA手动在线切换主 原创3(主不参与复制)
monitor 执行:slave2连接到slave1,server1 不做(主/从复制角色,停在那里) [root@monitor app1]# masterha_master_switch --co ...
- Redis 键(key)
Redis 键命令用于管理 redis 的键. 语法 Redis 键命令的基本语法如下: redis 127.0.0.1:6379> COMMAND KEY_NAME 实例 redis 12 ...
- idl 批量裁剪代码
PRO Subset_via_shp_update COMPILE_OPT idl2 ENVI,/restore_base_save_files envi_batch_init,LOG_FILE='b ...
- Github 的一些基本操作
1.创建一个新的repository: 先在github上创建并写好相关名字,描述.例如这样一个地址: https://github.com/test/test2.git 回到本地目录如hellowo ...
- android scrollview主要的问题
项目做多了之后,会发现其实 ScrollView嵌套ListVew或者GridView等很常用,但是你也会发现各种奇怪问题产生.根据个人经验现在列出常见问题以及代码最少最简单的解决方法. 问题一 : ...
- 为Google Reader守夜。。。
Google的阅读器快要关闭了... 立刻截图留恋呢,以后就没机会了. 唉,真是令人惋惜. 虽然我接触Google Reader还不到一年,但是我已经习惯当连上WiFi时马上更新一下手机上的gRead ...
- hdu 4267 树形DP
思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...
- ListView使用自定义适配器的情况下实现适配器的文本和图标控件点击事件执行Activity界面中的方法
ListView使用的是自定义适配器,列表项的布局文件中含有文本和图标,实现文本区域和图标区域的点击事件. 实现思路:在自定义适配器MyArrayAdapter 类型中自定义接口和接口方法,分别设置文 ...
- FindControl 无法找到控件问题解决方案
若用 string cdept =((HtmlInputText)FindControl("dept0" + i.ToString())).Value; 提示结果为空值,即无法找到 ...