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. 2018 Multi-University Training Contest 1 H - RMQ Similar Sequence(HDU - 6305 笛卡尔树)

    题意: 对于一个序列a,构造一个序列b,使得两个序列,对于任意的区间 [l, r] 的区间最靠近左端点的那个最大值的位置,并且序列 b 满足 0 < bi < 1. 给定一个序列 a ,求 ...

  2. 最短路径:HDU2006-一个人的旅行(多个起点,多个终点)

    题目: 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 遗传算法 | Java版GA_TSP (2)

    嗯哼,上一篇博客中用Java实现了遗传算法求解TSP(Java版GA_TSP(我的第一个Java程序)),但明显求解效果不太好,都没太好意思贴出具体的结果,今天捣腾了下,对算法做了一些小改进,求解效果 ...

  4. Unable to execute dex: Multiple dex files define Lcom/myapp/R$array;

    Unable to execute dex: Multiple dex files define Lcom/myapp/R$array; 我这个问题最后解决方式是,吧工程里面用同一个v4包. 很明显, ...

  5. python如何合并两个字典

    我有两个Python字典,如何合并它们呢?update()方法正是你所需要的. >>> x = {'a':1, 'b': 2} >>> y = {'b':10, ' ...

  6. 虚拟架构就绪 | 谈谈Windows Server 2012 R2迁移这件小事

    我们所说的“新选择”包括操作系统升级——告别Windows Server 2003或2008,选择用什么样的姿势进行升级呢? 新年伊始,正是企业对自身IT基础设施进行评估的最佳时期.在多项评估项目里面 ...

  7. 设计模式之第9章-原型模式(Java实现)

    设计模式之第9章-原型模式(Java实现) “快到春节了,终于快放假了,天天上班好累的说.”“确实啊,最近加班比较严重,项目快到交付了啊.”“话说一到过节,就收到铺天盖地的短信轰炸,你说发短信就发吧, ...

  8. RT-Thread学习之——静态线程和动态线程

    RT-Thread中支持静态和动态两种定义方式. 用线程来举例的话,rt_thread_init对应静态定义方式,rt_thread_create对应动态定义方式. 使用静态定义方式时,必须先定义静态 ...

  9. Unity 脚本<1>

    RaycastHit2D hit = Physics2D.Linecast(targetPosition, targetPosition + new Vector2(x, y)); 猜测是lineca ...

  10. Kernel Space与User Space(转)

    对于刚刚接触Linux的菜鸟来说,可能会不理解大家常说的Kernel Space和User Space是什么意思,我简单搜了一下,发现阮一峰写过一个比较简洁的介绍,贴下来给大家: 学习 Linux 时 ...