https://oj.leetcode.com/problems/palindrome-partitioning-ii/

给定一个串,让把它划分成子串,要求每个子串都是回文的。

动态规划:

设数组 ans(i)表示从0到 i 要经过的最小划分数,则

ans[i] = ans[k] + 1,如果 k 到 i 是回文的

            i - 1       ,如果 k 到 i 都不是回文的

class Solution{
public:
int minCut(string s)
{
const int n = s.size();
vector<int> ans;
ans.resize(n+);
vector<vector<bool> > isPal;
isPal.resize(n);
for(int i = ;i<n;i++)
isPal[i].resize(n); for(int i = ;i<=n;i++)
{
ans[i] = n - - i;
} for(int i = n-;i>=;i--)
for(int j = i; j<n;j++)
{
if(s[i] == s[j] &&(j-i< || isPal[i+][j-]))
{
isPal[i][j] = true;
ans[i] = min(ans[i],ans[j+]+);
}
}
return ans[];
}
};

LeetCode OJ-- Palindrome Partitioning II ***的更多相关文章

  1. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  2. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [Leetcode][JAVA] Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. leetcode 132. Palindrome Partitioning II ----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. Java for LeetCode 132 Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. Leetcode 132. Palindrome Partitioning II

    求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...

  7. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  8. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  10. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. visual studio cl -d1reportSingleClassLayout查看内存f分布

    C:\Users\Administrator\Desktop\cppsrc>cl -d1reportSingleClassLayoutTeacher virtual.cpp 用于 x86 的 M ...

  2. [BZOJ1588]营业额统计(Splay)

    Description 题意:给定 n个数,每给定一个数,在之前的数里找一个与当前数相差最小的数,求相差之和(第一个数为它本身) 如:5 1 2 5 4 6 Ans=5+|1-5|+|2-1|+|5- ...

  3. 51nod_1459 最短路 dijkstra 特调参数

    好多基础知识都没补完,只好看到.用到一个赶紧补全一个,并且保证下次需要的时候直接用,不用回来再补: 其实这个算法是在补同余最短路的时候用到的,当时突然发现理解算法导论上的原理甚至有效性证明,但是就是没 ...

  4. Android 完美解决bundle实现页面跳转并保留之前数据+传值

    1.前言 前言: 昨天碰到了一个问题,我想实现页面跳转,采用了Bundle之后,再回到原来的页面,发现数据也没有了, 而且一直报错,网上查找了很多资料,发现要用一个startActivityForRe ...

  5. Python协程详解(二)

    上一章,我们介绍了Python的协程,并讲到用yield达到协程的效果,这一章,我们来介绍yield from的结构和作用 我们先来对比下yield和yield from的用法 def first_g ...

  6. JS的跨域理解

    前言 周一的学院点开题被批的很惨,换了个校长,各种被抓严,班上已经有两个同学打算休学了.哎,这周的聚会可能是大家集聚的最后一次吧.熬着吧,还是学习我的前端,不管老板学校咋逼了,找个好工作才是王道.今天 ...

  7. 友推在Android 实现微信等分享代码的常见问题

    介绍,最近 做了一个项目,需要集成分享功能.果断选择 友推. 集成过程,参考友推官方提供的集成文档即可 废话不多说,主要说一下自己在集成过程中遇到的一些问题,主要有两个: 问题1. 引入youtui- ...

  8. IOS开发学习笔记006 - 指针

    C语言 指针 简单使用如下: int *p;//定义 int a = 10; p = &a;//赋值 int * b = &a;//定义并赋值 定义格式:类型 *指针变量名: 注意事项 ...

  9. 菜鸟之路——git学习及GitHub的使用

    首先,感谢廖雪峰老师的git教程 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 ...

  10. Repeater嵌套(灵活的)

    页面代码 <form id="form1" runat="server"> <asp:Repeater ID="rptCategor ...