Leetcode: Longest Palindromic Substring. java
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 String longestPalindrome(String s) {
if (s == null || s.length() == 0) return "";
int n = s.length();
int max = 0, start = 0, end = 0;
boolean[][] c = new boolean[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = i >= j ? true : false;
}
}
//c[i][j] 记录从第i个到第j个是不是回文。
for (int j = 1; j < n; j++) {
for (int i = 0; i < j; i++) {
if (s.charAt(i) == s.charAt(j) && c[i+1][j-1]) {
c[i][j] = true;
if (j - i + 1 > max) {
max = j - i + 1;
start = i;
end = j;
}
}
else
c[i][j] = false;
}
}
return s.substring(start, end+1);
}
}
Leetcode: Longest Palindromic Substring. java的更多相关文章
- [LeetCode] 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
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 ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- LeetCode——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题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- Leetcode:Longest Palindromic Substring分析和实现
问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...
随机推荐
- 什么时候用copy什么时候用retain (一)
在声明一个property的时候总是搞不清什么时候用retain,什么时候用copy,用上去了感觉也不会错,但是又没有安全感: Copy:顾名思义,复制,将对象复制一份,ios内部的操作时,先copy ...
- (win+linux)双系统,删除linux系统的条件下,删除grub引导记录,恢复windows引导
//(hdx,y) (显示查找到的分区号)第一个数字指第几个硬盘,第二个指第几个分区. 一般我们是(hd0,0) \n Linux的分区已经被你从Windows中删除,系统启动后停在“grub&g ...
- java多态中哪些成员具备多态特性
在多态的学习中,当子类继承父类时,子类中的变量哪些具备多态特性,哪些不具备多特特性. 代码: class Father{ public static int x=10; public int y=11 ...
- 网络加速手段之一,JS文件资源合并下载
有过ftp下载文件经验的人都有体验,单独下载一个100M的文件比分开下载100个1M的文件速度快很多,这是因为下载需要解析域名,建立连接等额外开销的时间,因此下载100个文件就要做100次这种额外的开 ...
- .net发邮件 附件文件名乱码
.net发邮件 附件文件名乱码,可以下载以下补丁安装. https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.asp ...
- Markdown 是什么?
这是一篇 Markdown 学习笔记,简要记录常用 Markdown 语法. Markdown 是什么? Markdown 是一种轻量级标记语言,创始人为约翰·格鲁伯(John Gruber)和亚伦· ...
- 《Linux命令行与shell脚本编程大全》 第一、二章 学习笔记
第一章:初识Linux shell Linux内核负责以下4个主要功能: 1.系统内存管理 2.软件程序管理 3.硬件设备管理 4.文件系统管理 1.系统内存管理 内核不仅管理服务器上的可用物理内存, ...
- 20151007kaggle Titanic心得
Titanic是kaggle上一个练手的比赛,kaggle平台提供一部分人的特征,以及是否遇难,目的是预测另一部分人是否遇难.目前抽工作之余,断断续续弄了点,成绩为0.79426.在这个比赛过程中,接 ...
- CentOS7 network
- 2d网络游戏的延迟补偿(Lag compensation with networked 2D games)
http://gamedev.stackexchange.com/questions/6645/lag-compensation-with-networked-2d-games ——————————— ...