[LeetCode]切割字符串,使各个子串都是回文
题目:Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
思路:
用数组dp[i]表示从下标i开始的字符串分割为回文子串的最少个数,
paliin[i][j] 表示从下标i到下标j是否为回文串,
利用动态规划
dp[i] = min(dp[i], 1 + dp[j]) if (palin[i + 1][j - 1] == ture or j - i < 2 ) && s[i] = s[j] , i <= j < len ,
i从len - 1递减至0
总的时间复杂度为O(n^2)
code
class Solution {
public:
int minCut(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.size();
int dp[len + ];//记录从i开始的最短切割次数
bool palin[len][len];//记录palin(i,j)是否为回文
//初始化dp[i]
for(int i = ; i <= len; ++i)
dp[i] = len - i;
//初始化palin[i][j]
for(int i = ; i < len; ++i)
for(int j = ; j < len; ++j)
palin[i][j] = false;
//从后往前计算dp[i],
for(int i = len - ; i >= ; --i)
for(int j = i; j < len; ++j)
{
if(s[i] == s[j] && (j - i < || palin[i + ][j - ]))
{
palin[i][j] = true;
dp[i] = min(dp[i], dp[j + ] + );
}
}
return dp[] - ;
}
};
[LeetCode]切割字符串,使各个子串都是回文的更多相关文章
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- 【leetcode 简单】第三十三题 验证回文串
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...
- LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium
题目: Given a , and there exists one unique longest palindromic substring. 解题思路:1.简单思路:暴力破解法,时间复杂度O(n^ ...
- 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...
- [LeetCode] Find the Closest Palindrome 寻找最近的回文串
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- LeetCode 9、判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
class Solution: def isPalindrome(self, x: int) -> bool: a = x if a<0: return False else: num = ...
- C#实现:给定任意要给字符串,输出所有可能的回文的子字符串集合。
class Program { static void Main(string[] args) { string testStr = "sdfadfdsfadfdsfsdf"; i ...
随机推荐
- bzoj 1318: [Spoj744] Longest Permutation 智商题
1318: [Spoj744] Longest Permutation Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 361 Solved: 215 ...
- CodeForces 300A Array
http://codeforces.com/problemset/problem/300/A 题意 :给你n个数字,让你分成3组,第一组各个数之积要小于0,第二组要大于0,第三组要等于0,符合要求的答 ...
- linux 5.5 开xmanager远程
http://bbs.cqsztech.com/dv_rss.asp?s=xhtml&boardid=3&id=11&page=9 linux 5.5 开xmanager远程 ...
- PowerDesigner15(16)在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
1.用PowerDesigner15建模,在Database—>Generate Database (或者用Ctrl+G快捷键)来生产sql语句,却提示“Generation aborted d ...
- mybatis知识点
1.Mybatis比IBatis比较大的几个改进是什么 a.有接口绑定,包括注解绑定sql和xml绑定Sql , b.动态sql由原来的节点配置变成OGNL表达式, c. 在一对一,一对多的时候引进了 ...
- Eclipse中安装使用SVN
参考网址: Eclipse中使用SVN - 流逝的是岁月,沉淀的是经典 - 博客频道 - CSDN.NET http://blog.csdn.net/v123411739/article/detail ...
- 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)
当 Adobe.Microsoft.Sun 等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 现今存在的开源协议很多,而经过 Open Source Initiative 组织通过批 ...
- CSS+DIV 布局三种定位方式
一.普通流 普通流中元素框的位置由元素在HTML中的位置决定.块级元素从上到下依次排列,框之间的垂直距离由框的垂直margin计算得到.行内元素在一行中水平布置. 二.定位 1.相对定位 被看作普通流 ...
- 转XMLHelper
http://www.cnblogs.com/lixyvip/archive/2009/09/16/1567929.html using System; using System.Collection ...
- objective C中数据持久化方式1--对象归档
第一.数据持久化的方式: NSKeyedArchiver--对象归档 属性列表化(NSArray.NSDictionary.NSUserDefault) SQlite数据库.CoreData数据库 其 ...