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度旋转后,变成了一个与原数字不同的 ...
随机推荐
- .net Core使用 MongoDB
1.安装mogodb windows版本下载地址:https://www.mongodb.com/download-center/v2/community 查看mongod.conf文件,找到绑定的I ...
- H5的pushState与replaceState的用法
一.简介 HTML5引入了 history.pushState()和 history.replaceState()方法,它们分别可以添加和修改历史记录条目.这些方法通常与window.onpopsta ...
- 字符串匹配(KMP&BF)
字符串匹配 题目描述 设计一个程序,从一个主字符串中查找一个子字符串在主串中第一次出现的位置.主串和子串的长度不超过100.如果找不到,则输出-1. 程序输入说明 第一行输入一个整数N,说明需要进 ...
- VUE -- Identifier 'n_type' is not in camel case
Identifier 'n_type' is not in camel case 参数名的 `_` 去掉就好了
- python将py文件转换为pyc
python -m py_compile lib/ylpy.py python -m py_compile lib/ylpy.py python 一个.py文件如何调用另一个.py文件中的类和函数 A ...
- JAVA基于File的基本的增删改查
直接上代码 public class TestFile { /** * 创建目录 * @param filename 路径 */ public static void createFile(Strin ...
- python3 django项目从项目中导出依赖包
1. 在项目的根目录中使用mac终端执行命令, pip3 freeze > requirements.txt #requirements.txt只是个名字可以随便起,一般默认为requireme ...
- appStore上传苹果应用程序软件发布
首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Portal,然后点击DisTribution(1)图中加号是灰色 ...
- Java同步数据结构之ConcurrentLinkedQueue
前言 前面介绍的Queue都是通过Lock锁实现的阻塞队列,今天介绍一种非阻塞队列ConcurrentLinkedQueue,所谓非阻塞,其实就是通过CAS代替加锁来实现的高效的非阻塞队列.当许多线程 ...
- 设置Win10默认启动的Linux子系统版本,启动指定Linux发行版
设置Win10默认启动的Linux子系统版本,启动指定Linux发行版 MS酋长一年前已经与大家分享了启用“适用于Linux的Windows子系统(WSL)”的方法,但当时所能安装的只有由Cano ...