leetcode 算法整理
一 字符串中的最大回文串(第5题)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of sis 1000.
Example:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example:
Input: "cbbd" Output: "bb"
1. 我的解法(accepted): 中心扩展
思路: 回文即代表有中心,一次遍历中,对于每个位置上的数字求一下最大的回文串即可,初始处理剪枝,合并掉同样的元素,如xxxxaaaxxxx,先常量级别把a合并掉; 遍历时候再次剪枝,遍历过的有一个maxLen,如果即将遍历的元素最大可能回文长度都不可能超过maxLen,不再遍历。
public class Test1218 {
public static void main(String[] args) {
String str = "ababababa";
System.out.println(longestPalindrome(str));
}
public static String longestPalindrome(String s) {
if (s == null) {
return null;
}
char[] chars = s.toCharArray();
int length = chars.length;
int maxLen = 0;
String maxStr = "";
for (int i = 0; i < length; i++) {
// cut branch
int possibleLength = getMaxPossibleLength(i, length);
if (possibleLength < maxLen) {
continue;
}
String maxStrTmp = getMaxStrByIndex(i, chars);
if (maxLen < maxStrTmp.length()) {
maxLen = maxStrTmp.length();
maxStr = maxStrTmp;
}
}
return maxStr;
}
private static int getMaxPossibleLength(int index, int length) {
int head = 0;
int tail = length - 1;
if (index == head || index == tail) {
return 1;
}
int result1 = index - head;
int result2 = tail - index;
int min = result1 <= result2 ? result1 : result2;
return min * 2 + 1;
}
private static String getMaxStrByIndex(int index, char[] chars) {
StringBuilder sb = new StringBuilder(String.valueOf(chars[index]));
int length = chars.length;
int head = index - 1;
int tail = index + 1;
// middle deal
while (true) {
if (head >= 0 && chars[index] == chars[head]) {
sb.insert(0, String.valueOf(chars[head--]));
} else if (tail <= length - 1 && chars[index] == chars[tail]) {
sb.append(String.valueOf(chars[tail++]));
} else {
break;
}
}
// besides deal
while (true) {
if (head < 0 || tail > length - 1) {
break;
}
if (head >= 0 && tail <= length - 1 && chars[head] == chars[tail]) {
sb.insert(0, String.valueOf(chars[head--]));
sb.append(String.valueOf(chars[tail++]));
continue;
}
break;
}
return sb.toString();
}
}
2. dp解法
思路: 设 p[i][j] 代表下标从i至j的子字符串是否是回文串,取值为boolean
转移方程 p[i][j] = p[i+1][j-1] && chars[i] == chars[j]
初始化的状态为 p[i][i] = true p[i][i+1] = chars[i] == chars[i+1]
看下面手绘图理解一下,打勾的对角线p[i][i] 恒为true, 只有这一列是不够状态转移的,因为按照转移方程,必须要图示的↗️方向递推,那么要需要有圆圈的一列初始化,这列对应的是p[i][i+1]。 剩下的递推即可,比如图中的箭头栗子
leetcode 算法整理的更多相关文章
- Leetcode——回溯法常考算法整理
Leetcode--回溯法常考算法整理 Preface Leetcode--回溯法常考算法整理 Definition Why & When to Use Backtrakcing How to ...
- Leetcode——二叉树常考算法整理
二叉树常考算法整理 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Leetcode算法项目点star呀~~ 二叉树常考算法 ...
- BFS与DFS常考算法整理
BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或 ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- LeetCode算法题-Subdomain Visit Count(Java实现)
这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...
- LeetCode算法题-Number of Lines To Write String(Java实现)
这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...
- LeetCode算法题-Unique Morse Code Words(Java实现)
这是悦乐书的第318次更新,第339篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是804).国际莫尔斯电码定义了一种标准编码,其中每个字母映射到一系 ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- LeetCode算法题-Rotated Digits(Java实现)
这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...
随机推荐
- EGL Driver message (Critical) eglInitialize: No available renderers.
使用Python的selenium库进行自动化巡检.并将相对应的数据保存 环境: Windows Embedded Standard Python 2.7.16 selenium 3.141.0 [0 ...
- redhat7.4安装git(按照官网从源码安装)
按照官方文档建议使用源码安装 1.为什么不用yum安装 yum安装确实简单,只用一行命令就可以了,但是yum安装的版本太低. //安装前使用info查看git版本信息等 yum info git yu ...
- springboot的注解
1.@ConfigurationProperties 功能:装载配置文件信息到实体 原理:aop,通知类型:?(方法或对象创建完后) 注意:作用于方法上可以不需要改源码(例如durid配置) 转载:h ...
- intel官方的手册
最近在学习汇编语言,需要用到intel的手册,无论是csdn还是其他的,都要下载币,还不便宜,也很老的资料了. 直接到这个地址:https://software.intel.com/en-us/art ...
- jboss虚拟机快照
若使用jboss,则一定要记得隔断时间拍一个快照,这样如果虚拟机坏了,则可以进行回复快照,避免再次花时间去解决问题. 拍快照:启动虚拟机,然后,操作如下. 选择,current State,右击,则可 ...
- 微信小程序之--(与唯品会来场粉红色的邂逅 ???)
Welcome to miaomiaoXiong's segmentfault 微信小程序之--(与唯品会来场粉红色的邂逅 ???) 买买买,虽然双十二刚过,可是唯品会的折扣却是依然火爆.一打开页面, ...
- Hearthstone AI
search keyword `machine learning hearthstone` with google I am a legend: Hacking Hearthstone with ma ...
- LeetCode —— 单词接龙(Python)
使用字典,降低查找的复杂度.使用list会超时. class Solution: def nextWordsList(self, word, wordDict): res_list = [] for ...
- [dart学习]第五篇:操作符
前言:本系列内容假设读者有一定的编程基础,如了解C语言.python等. 本节一起来学习dart的操作符,直接拷贝官网的操作符描述表如下: Description Operator unary pos ...
- Python查询Mysql时返回字典结构的代码
Python查询Mysql时返回字典结构的代码 MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.D ...