LeetCode OJ-- Palindrome Partitioning II ***
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 ***的更多相关文章
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [Leetcode][JAVA] Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 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 ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- 关于欧几里德算法(gcd)的证明
求a,b的最大公约数我们经常用欧几里得算法解决,也称辗转相除法, 代码很简短, int gcd(int a,int b){ return (b==0)?a:gcd(b,a%b); } 但其中的道理却很 ...
- MySQL创建数据库及用户
create database ${db_name} default charset utf8 COLLATE utf8_general_ci; grant all on ${db_name}.* t ...
- spark 对hbase 操作
本文将分两部分介绍,第一部分讲解使用 HBase 新版 API 进行 CRUD 基本操作:第二部分讲解如何将 Spark 内的 RDDs 写入 HBase 的表中,反之,HBase 中的表又是如何以 ...
- Android BadgeView 工具包
前言:消息未读,显示红点或者红色数字,其实就是一个TextView,有推送一般就有badgeView. Step 1 因为在github上看到了一些类似的第三方库,嫌麻烦,不如直接封装一个类,直接使用 ...
- 随手正则写的 CSDN【只看楼主】功能
写这个的时候居然没有看到原来CSDN已经有这个功能了,写完代码了突然发现原来早就已经有了. 现把代码贴出来吧,虽然有很多解析HTML的开源类库如:http://htmlagilitypack.code ...
- Python数据结构之列表、元组及字典
一位大牛Niklaus Wirth曾有一本书,名为<Algorithms+Data Structures=Programs>,翻译过来也就是算法+数据结构=程序.而本文就是介绍一下Pyth ...
- IOS开发---菜鸟学习之路--(十七)-利用UITableView实现个人信息界面
首先来看下我们要实现的效果 需要实现这样的效果 然后我们开始动手吧. 首先选择添加一个新的ViewController 然后打开XIB文件,添加一UITableView 并将样式设置为分组 同时将按住 ...
- Uncaught TypeError: Cannot read property of undefined In JavaScript
当脚本遇到未初始化的变量或对象时,通常会抛出如上图所示的错误. Decription 'Undefined'是全局对象的属性.如果没有为变量赋值,则为'undefined'类型.当求值变量没有任何赋值 ...
- poj3748 位运算 bitset
位操作 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9064 Accepted: 3626 Description 假 ...
- linux文件备份到windows方法
目录 背景 方案 过程记录 在windows上创建共享目录 将windows上共享的目录绑定到/mnt目录下 问题处理 背景 需编写部门wiki备份数据脚本.但wiki部署在linux上,而需将备份数 ...