[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 ...
随机推荐
- [转]StructLayout特性
转自:http://www.cnblogs.com/JessieDong/archive/2009/07/21/1527553.html StructLayout特性 StructLayout特性 ...
- Form表单学习网站
HTML表单 -- form标签 -- 与浏览者交互:http://www.dreamdu.com/xhtml/tag_form/
- 手工、工具分别实现cookie注入
最开始的判断access类型的网站注入点可以用“1 and 1=1”来判断. 不过现在的网站基本上被挡住了.之后呢,可以考虑cookie注入. Dim Tc_Post,Tc_Get,Tc_In,Tc_ ...
- 1961-计算机基础知识大赛 2 (new)
描述 求A^B的最后三位数表示的整数(1<=A,B<=10000) 输入 A B 输出 A^B的最后三位数 样例输入 2 3 12 6 样例输出 8 984 #include<ios ...
- Amzon MWS API开发之订单接口
Amazon订单接口是Amazon MWS 开发接口中的一大块,我们可以通过接口调用来获得订单数据. 在调用接口之前,首先我们要获得相关店铺商家的店铺密钥等信息.如下: 在此我将所有信息定义在一个类中 ...
- 使用Data Annotations进行手动数据验证
Data Annotations是在Asp.Net中用于表单验证的 它通过Attribute直接标记字段的有效性,简单且直观.在非Asp.Net程序中(如控制台程序),我们也可以使用Data Anno ...
- 【疯狂Java学习笔记】【第一章:Java语言概述】
[学习笔记]1.Java与C#的异同:Java与C#的相同之处有很多,它们均摒弃了C++面向对象的多继承.宏定义.全局变量.全局函数.指针等等难以使用的机制,添加进了成熟的机制,如垃圾回收.接口等等. ...
- http://www.cnblogs.com/zhwl/p/3642486.html
http://www.cnblogs.com/zhwl/p/3642486.html http://blog.csdn.net/hpf911/article/details/14165865
- java 转换 小函数(不断增加中。。。)
//char数组转换成byte数组 private byte[] getBytes (char[] chars) { Charset cs = Charset.forName ("UTF-8 ...
- Android的三种网络通信方式
Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口).Org.apache接口和Android.net.*(Android网络接口).下面分别介绍这些接口的功 ...