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 ...
随机推荐
- Flask初学者:配置文件
如果设置项比较少的话可以使用“app.config['param_name']=value”的形式直接使用,如果需要设置的参数比较多的话,可以单独新建一个配置文件用来存放配置信息,配置文件中的参数需大 ...
- Essential C++ 3.1 节的代码练习——指针方式
// // PointerToValue.cpp // Working // // Created by Hawkins, Dakota Y on 6/3/16. // Copyright 2016 ...
- Sonya and Robots CodeForces - 1004C (思维题)
Sonya and Robots time limit per test 1 second memory limit per test 256 megabytes input: standard in ...
- linux学习(二) -- ubuntu下lnmp环境的配置
亲测的教程,,希望能对大家提供些许帮助,转载请注明出处 ubuntu+nginx+mysql+php7 一.安装Nginx 1.首先添加nginx_signing.key(必须,否则出错) $ wge ...
- IOS开发学习笔记027-UITableView 使用模型对象
1.模型对象 2.单组数据的显示 1.模型对象 继续优化上一个程序 上一次用到字典,但是坏处多多.这里将这些数据封装到类中. 这就是MVC中得模型,模型就是数据的显示结构 新建一个类,添加几个属性和一 ...
- python学习之dictionary函数的用法
编写下面这段代码运行出现了报错.#!/usr/bin/env python2.7#-*-coding:utf-8 -*- d=['T']a=raw_input('请输入a的值')if a in d : ...
- android抓取logcat日志的方法
这几天帮忙测试一个APP,报告结果需要提交日志文件,于是百度了下安卓的获取日志方法,其实很简单,7个步骤搞定,下面把我的总结分享给大家. 1.下载adb工具包 https://pan.baidu.co ...
- c++ 中的slipt实现
来自 http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html http://blog.diveinedu.com/%E4%B8%89%E7%A7%8D%E5 ...
- [cocos2dx UI] CCLabelAtlas 为什么不显示最后一个字
CClabelAtlas优点,基本用法等我就不说了,这里说一个和美术配合时的一个坑!就是图片的最后一位怎么也不显示,如下图中的冒号不会显示 查了ASCII码表,这个冒号的值为58,就是在9(57)的后 ...
- Log4j官方文档翻译(九、输出到数据库)
log4j提供了org.apache.log4j.JDBCAppender对象,可以把日志输出到特定的数据库. 常用的属性: bufferSize 设置buffer的大小,默认是1 driver 设置 ...