leetcode — longest-palindromic-substring
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/longest-palindromic-substring/
*
* Created by lverpeng on 2017/6/28.
*
*
* 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 LongestPalindromicSubString {
/**
* 给定一个字符串,从给定位置开始向两边找出其中的回文字符串
*
* @param str
* @param left
* @param right
* @return
*/
public String findPalindrome (String str, int left, int right) {
int n = str.length();
while (left >= 0 && right <= n - 1 && str.charAt(left) == str.charAt(right)) {
left --;
right ++;
}
return str.substring(left + 1, right);
}
/**
* 依次循环字符串的每一个元素,以钙元素为中心寻找最长的回文字符串
* 分奇数、偶数两种情况考虑
*
* @param str
* @return
*/
public String longestPalindromicStrByForce (String str) {
String lonestStr = "";
for (int i = 0; i < str.length(); i++) {
// 奇数的情况
String subStr = findPalindrome(str, i, i);
if (lonestStr.length() < subStr.length()) {
lonestStr = subStr;
}
subStr = findPalindrome(str, i, i+1);
if (lonestStr.length() < subStr.length()) {
lonestStr = subStr;
}
}
return lonestStr;
}
public boolean isPalindromicStr (String str) {
int length = str.length();
if (length % 2 == 0) {
return findPalindrome(str, length / 2 - 1, length / 2).length() == length;
}
return findPalindrome(str, length / 2, length / 2).length() == length;
}
/**
* 起始位置为i,依次判断i-j(j去从length到i之间的数,这样背刺只要找到一个就是,从i开始的最长的会问字符串了,直接跳出本次循环)之间的字符串是不是回文字符串,如果是的话和目前最长的会问字符串比较,找到新的最长
*
* @param str
* @return
*/
public String longestPalindromicStrByForce1 (String str) {
String longestStr = "";
for (int i = 0; i < str.length(); i++) {
for (int j = str.length() - 1; j >= 0; j--) {
String subStr = str.substring(i, j);
if (isPalindromicStr(subStr)) {
if (longestStr.length() < subStr.length()) {
longestStr = subStr;
}
break;
}
}
}
return longestStr;
}
/**
* 动态规划:把原问题分解为相对简单的子问题 求解复杂问题的方法
* 记忆化的求解递推式
* 1. 求出所有子字符串,遍历字符串,每次遍历,找到0-i之间所有的子字符串,针对每个子字符串判断是不是回文字符串
* 2. 针对0-i之间的字符串,从i一个字符开始计算,然后i-1,i
* i
* i-1, i
* i-2, i-1, i
* i- 3, i-2, i-1, i
* 总结得出:如果p(j, i)是一个回文字符串的话,那么p(j+1, i-1)一定是
* 那么递推式就是:p(j, i) <= str[j] == str[i] && p(j+1, i-1),p(x,y)str[x]-str[y]之间的字符串是回文字符串
* 在上面的计算中,计算p(j, i)的时候,已经计算过p(j+1, i-1),那么就可以把p(j+1, i-1)缓存起来,不需要重复计算
*
* 动态规划解题方法:
* 分解子问题,找到状态转移方程
*
* 概念:
* 状态:表示问题的中间结果
* 阶段:同时可能有多个中间结果
* 决策:一个状态转移到另一个状态,往往通过决策来进行,有决策就会有状态转移
* 无后效性:一旦某一个状态确定之后,它之前的状态无法对他之后的状态产生影响
* 最优子结构:最优解包含的子问题的解也是最优的,就称该问题具有最优子结构,也就是满足最优化原理
*
* 动态规划适用范围:
* 最优子结构
* 无后效性
* 子问题的重叠性质
*
* 与贪心算法的区别:
* 贪心算法:采取当前状态下最好的选择,以期导致结果是最好的
* 贪心算法对每个子结果做出选择,不能回退,也就是只依赖前一个结果
* 动态规划会依赖原来所有的结果,需要保存原来的结果,根绝原来的结果对当前结果进行选择,有回退功能
*
*
* 使用动态规划来计算
* 上面使用暴力法1的时候,在计算i-j的时候已经判断过i--, j++,所以可以把i--, j++保存下来,下次就不用重新计算判断了
*
* @param str
* @return
*/
public String longestPalindromicStrByDp (String str) {
String longestStr = "";
boolean[][] table = new boolean[str.length()][str.length()];
for (int i = 0; i < str.length(); i++) {
for (int j = i; j >= 0; j--) {
// i == j 说明j-i之间只有j、i两个元素,一定是回文的,true
// i、j两个未位置的字符相等,j++、i--两个字符也相同,并且i- j < 2(这个时候只有j、i两个位置的字符),
// 或者是j++, i__两个位置的字符也相同(j++,i--前面已经计算过,每次保证两个位置和向内两个未知的字符分别相同,
// 那么最后一定是回文字符串)
if (i == j || (str.charAt(i) == str.charAt(j) && (i - j < 2 || table[j+1][i-1]))) {
table[i][j] = true;
if (longestStr.length() < i - j + 1) {
longestStr = str.substring(j, i + 1);
}
}
}
}
for (int i = 0; i < table[0].length; i++) {
System.out.println(Arrays.toString(table[i]));
}
return longestStr;
}
public static void main(String[] args) {
LongestPalindromicSubString longestPalindromicSubString = new LongestPalindromicSubString();
String s = "abacdfgdcaba";
System.out.println(longestPalindromicSubString.longestPalindromicStrByForce(s));
System.out.println(longestPalindromicSubString.longestPalindromicStrByForce(s));
System.out.println(longestPalindromicSubString.longestPalindromicStrByDp(s));
}
}
leetcode — longest-palindromic-substring的更多相关文章
- [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. java
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题解(动态规划)
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)级别的时间 ...
随机推荐
- ECMAscript,DOM,BOM哪个比较重要
ECMA > DOM > BOM ECMA是JS的核心,语法.变量.对象.各种对象的API.作用域.原型链.闭包.This指向.构造函数等等必须要熟练,有了这些基础你才能去熟练的操作DOM ...
- 九校联考_24OI——餐馆restaurant
凉心模拟D1T1--最简单的一道题 TAT 餐馆(restaurant) 题目背景 铜企鹅是企鹅餐馆的老板,他正在计划如何使得自己本年度收益增加. 题目描述 共有n 种食材,一份食材i 需要花ti 小 ...
- cpp 区块链模拟示例(二)工程代码解析
/* 作 者: itdef 欢迎转帖 请保持文本完整并注明出处 技术博客 http://www.cnblogs.com/itdef/ 技术交流群 群号码:432336863欢迎c c++ window ...
- JavaScript:event loop详解
之前已经有两篇随笔提到了event loop,一篇是事件机制,一篇是tasks和microtasks,但是里面的event loop都是文字描述,很难说细,逻辑也只是简单的提了一遍.其实之前也是通过阮 ...
- java生成pdf文件 --- Table
Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...
- python open()函数的模式选择
python open()函数打开文件的模式详解 使用python处理文件时,避免不了要用到open()函数.我们今天主要讨论mode参数的区分. fd = open('文件名(路径)’, mode= ...
- Linux 根据PID找到相应应用程序的运行目录
1.找到运行程序的PID # ps aux | grep redis root pts/ S+ : : grep redis root ? Ssl Aug30 : redis-server *: # ...
- datatable实例教程
网站的后台,多数是需要使用datatable来展示数据的,因为datatable的功能比较强大,可以更好的使用. 引用css <link href="../../static/asse ...
- EF6学习笔记(四) 弹性连接及命令拦截调试
EF6学习笔记总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 本章原文地址:Connection Resiliency and Command Interception 原文 ...
- Centos7.x gnome 桌面美化
一.管理工具 gnome是通过gnome-tweak-tool(优化工具)来管理的,可以在左上角的应用程序->工具里找到. 也可以直接在终端输入gnome-tweak-tool来启动它.启动界面 ...