leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2

大致与lc131相同,这里要求的是最小分割方案
同样可以分割成子问题
dp[i][j]还是表示s(i~j)是否为palindrome
res[i]则用来记录s(0~i)的最小cut数
class Solution {
public int minCut(String s) {
int[] dp = new int[s.length()];
boolean[][] isP = new boolean[s.length()][s.length()];
for(int i=0; i<s.length(); i++){
int min = i;
for(int j=0; j<=i; j++){
if(s.charAt(i) == s.charAt(j) && (i-j <= 1 || isP[j+1][i-1])){
isP[j][i] = true;
min = j==0 ? 0 : Math.min(min, dp[j-1]+1);
}
}
dp[i] = min;
}
return dp[s.length() - 1];
}
}
leetcode 132 Palindrome Pairs 2的更多相关文章
- LeetCode 336. Palindrome Pairs
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- leetcode 131 Palindrome Pairs
lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...
- leetcode@ [336] Palindrome Pairs (HashMap)
https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming
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 ...
随机推荐
- 4.RabbitMQ Linux安装
这里使用的Linux是CentOS6.2 将/etc/yum.repo.d/目录下的所有repo文件删除 先下载epel源 # wget -O /etc/yum.repos.d/epel-erlan ...
- Delphi常用字符串函数
Delphi常用字符串函数 一.字符转换函数1.ord(input[i])返回字符表达式 input 左端起第 I 字符的ASCII 码值.2.CHAR()将ASCII 码转换为字符.如果没有输入 ...
- Jeecg 容器初始化监听器
文件位置: F:\jeecg-bpm-3.8\jeecg-bpm-3.8-master\jeecg-bpm-3.8\src\main\java\org\jeecgframework\web\syste ...
- AsyncAwait 学习
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 『BASH』——Learn BashScript from Daniel Robbins——[001-002]
ABSTRACT: Daniel Robbins is best known as the creator of Gentoo Linux and author of many IBM develop ...
- 剑指offer——17数值的整数次方
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 保证base和exponent不同时为0 一般解法: 直接相乘: cl ...
- 3.2_springBoot2.1.x检索之JestClient操作ElasticSearch
这里介绍Jest方式交互, 导入jest版本 <!--导入jest--> <dependency> <groupId>io.searchbox</groupI ...
- 16-Ubuntu-文件和目录命令-切换目录-cd
cd(change directory),其功能为更改当前的工作目录. 注意:Linux所有的目录和文件名都是大小写敏感的. 命令 含义 cd 切换到当前用户的主目录(/home/用户目录) ...
- redhat7和redhat6混合搭建文档
1.下载cm对应redhat7的版本.2.修改/opt/cloudera/cm-5.10.0/etc/cloudera-scm-agent/config.ini成为中心机器hostname/ip(ma ...
- eclipse导出说明文档
选中项目--右键--Export--Java--Javadoc—Finish 1.为程序添加文档注释 2.选中项目--右键Export--Java--Javadoc--next, 3.next--在V ...