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的更多相关文章

  1. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  2. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

  3. leetcode@ [336] Palindrome Pairs (HashMap)

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  4. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

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

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

  6. [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 ...

  7. 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 ...

  8. Leetcode 132. Palindrome Partitioning II

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

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

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

随机推荐

  1. 创建 Angular 8.0 项目

    创建 Angular 8.0 项目,首先确保已经安装了 nodejs,如果没有安装,请看这篇文章安装:node.js 安装 1.新建一个空文件夹 angularproject,作为工作区 2.安装 A ...

  2. Openstack Paste.ini 文件详解

    目录 目录 pasteini 配置文件详解 composite pipeline filter app DEFAULT server Request 被 pasteini 处理的流程 如何加载 pas ...

  3. 阿里云ecs(phpstudy一件包)

            选择语言       保存并连接    Linux硬盘挂载是比较常见的管理操作之一.默认情况下数据盘没有挂载,需要手动挂载到系统中.     具体操作是分三步:     硬盘挂载1)需 ...

  4. iOS之NSArray数组排序

    一.数组遍历 除了常用的for和for-in遍历外,系统还提供了三种枚举遍历,对于大量的数据遍历可以使用下列三个方法. - (void)enumerateObjectsUsingBlock:(void ...

  5. java编程——数据的加法

    设计思想: 第一步:从键盘上输入一定个数的整数. 第二步:因为在main()方法中的参数是String类型的,所以第一步中输入的其实是字符类型,在这一步要把String转化成int. 第三步:初始化s ...

  6. Hadoop Pig组件

  7. STL之__ type_traits

    __type_traits:双底线是说明这是SGI STL内部使用的东西,不在STL标准范围之内.iterator_traits负责萃取迭代器(iterator)的特性.而__type_traits则 ...

  8. CF431E Chemistry Experiment

    题意:有n个试管,有高度为hi的水银.操作1:将试管x中的水银高度改成y.操作2:将体积为v的水注入试管,求水位的高度?n,q<=1e5. 标程: #include<bits/stdc++ ...

  9. 校园商铺-4店铺注册功能模块-10店铺注册之js实现

    1. 建立js目录和文件 1.1 建立js目录 在webapp下新建文件夹js,再在js目录下新建shop文件夹. 1.2 js文件 js的功能: 1.从后台获取到店铺分类.区域等是信息,将它填充到前 ...

  10. Windows copy

    将一份或多份文件复制到另一个位置. COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]     [+ source ...