IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> [LeetCode] Longest Palindromic Substring -> 正文阅读

[开发工具][LeetCode] Longest Palindromic Substring

Description

Longest Palindromic Substring: Given a string s, return the longest palindromic substring in s.

Example:

Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Solution

The basic idea for this question is to use dynamic programming. This is because whether a string is a palindrome can be transfered from whether its substring is a palindrome. For example, if a string abcba is palindrome, its substring bcb also is a palindrome. But if a substring bcb is a palindrome, a string abcbd may not be a palindrome.

Moreover, we notice that the odd length string can be transfered from its previous odd length substring, and the even length string can be transfered from its previous even length substring. For example, abcba which length is 5 can be transfered from bcb which length is 3. Also, abccba which length is 6 can be transfered from bccb which length is 4.

Accroding to these two analyses, we can get the transfer equation: dp[i][j] = dp[i + 1][j - 1] + 2 if dp[i + 1][j - 1] is a palindrome and s[i] == s[j]. (i and j are indexes of the string s)

Finally, the last thing we should do is to record the start and end of longest palindromic substring for return.

Code

class Solution {
public:
    string longestPalindrome(string s) {
        int len = s.length();
        int start = 0, end = 0;
        
        vector<vector<int>> dp(2, vector(len, 0));
        dp[0][0] = 1;
        for(int i = 1; i < len; i++) {
            dp[1 & 1][i] = 1;
            if(s[i] == s[i - 1]) {
                dp[2 & 1][i] = 2;
                start = i - 1;
                end = i;
            }
        }
        
        for(int curLen = 3; curLen <= len; curLen++) {
            for(int i = len - 1; i >= curLen - 1; i--) {
                if((dp[curLen & 1][i - 1] == curLen - 2) && s[i - curLen + 1] == s[i]) {
                    dp[curLen & 1][i] = curLen;
                    start = i - curLen + 1;
                    end = i;
                }
            }
        }
        return s.substr(start, end - start + 1);
    }
};

Complexity

Time complexity: O(n ^ 2)

Space complexity: O(n) for dynamic programming

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2021-08-06 10:02:19  更:2021-08-06 10:02:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 16:01:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码