LeetCode
来源:互联网 发布:淘宝服装拍摄后期处理 编辑:程序博客网 时间:2024/06/03 14:44
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Example 1:
Input:
"bbbab"Output:
4One possible longest palindromic subsequence is "bbbb".
Example 2:
Input:
"cbbd"Output:
2One possible longest palindromic subsequence is "bb".
Subscribe to see which companies asked this question.
题意:求一个字符串中的最长回文子串
思路:dp[i][j]表示字符串下标从 i ~ j 能构成的最长回文子串的长度,最终返回结果为dp[0][len - 1]。
class Solution {public: int longestPalindromeSubseq(string s) { int len = s.length(); vector<vector<int>> ans(len + 1, vector<int>(len + 1, 0)); for (int i = 0; i < len; i++) { ans[i][i] = 1; } for (int i = len - 1; i >= 0; i--) { for (int j = i + 1; j < len; j++) { if (s[i] == s[j]) { //相等时说明可以加入当前字符串中 ans[i][j] = ans[i+1][j-1] + 2; } else { //不等则只能去最大值 ans[i][j] = max(ans[i+1][j], ans[i][j-1]); } } } return ans[0][len-1]; }};
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- 实例011 猴子吃桃
- EIGRP认证
- 冒泡排序,选择排序,插入排序思想分析
- session.isNew()一直返回false(没作用,不执行)解决方案
- 对java和c语言之中进程间通信的理解
- LeetCode
- spring 定时任务的 执行时间设置规则
- Android广播之发送自定义广播+本地广播(二)
- Android Activity之间数据传递
- 嵌入式视频方案学习第三篇——Hi3518E SDK安装说明
- numpy中的数组切片
- 面试常见的排序问题
- 用SAXReader解析xml文档
- 实验一:Doolittle三角分解法之C语言代码