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度旋转后,变成了一个与原数字不同的 ...
随机推荐
- CF540D Bad Luck Island
嘟嘟嘟 看到数据范围很小,就可以暴力\(O(n ^ 3)\)dp啦. 我们令\(dp[i][j][k]\)表示这三种人分别剩\(i, j, k\)个的概率.然后枚举谁挂了就行. 这里的重点在于两个人相 ...
- Linux之信号
产生信号五种方法: 按键产生:ctrl+c.ctrl+z.ctrl+\ 系统调用产生:如kill.raise.baort 软件条件产生:如定时器alarm 硬件异常产生:非法访问内存(段错误).除0( ...
- 存在日期类型的JSON数据,进行SpringMVC参数绑定时存在的问题和解决方案
这篇文章已经过时了. 请参考比较合适的前后端交互方式. 首先是发送AJAX请求的html页面 <!DOCTYPE html> <html> <head> <m ...
- SpringMVC 指定404、500错误页面
1.在web.xml中追加 <error-page> <error-code>404</error-code> <location>/404</l ...
- 转:C++何时调用构造函数,何时调用析构函数
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/wjf1997/article/detai ...
- jvm方法栈
调用栈 先入后出 栈是一个只有一个口的容器,先进入栈的会落到栈底,出栈的时候最后出.最后进入栈的,在栈顶,出栈时先出. 方法调用时,需要在内存中开辟一块存储空间做为线程栈空间 每个线程都由自己的栈 调 ...
- oracle传输表空间
https://blog.csdn.net/ch7543658/article/details/39271135/ Oracle expdp/impdp常用性能优化方法 1.查看操作系统endiann ...
- Oracle11g安装与卸载教程
1.1,前言: 电脑太卡,鄙人穷屌丝啊,没钱买新电脑,想想周六日还要耍游戏就给电脑重做了个系统,糟糕的是电脑上的各种环境,工具都需要重新装一边,包括oracle数据库- -,依稀记得昨天装了一上午的数 ...
- [Scikit-learn] 2.3 Clustering - kmeans
参考: 2.3. Clustering 2.4. Biclustering 2.1.2.3. The Dirichlet Process Clusering, GMM, Variational Inf ...
- MVP模式的学习
简书: http://www.jianshu.com/p/f6fd9656f619 Github: https://github.com/tsaievan/YFMVPDemo