108.Palindrome Partitioning II-分割回文串 II(中等题)
来源:互联网 发布:在职软件工程硕士叫停 编辑:程序博客网 时间:2024/04/29 22:39
分割回文串 II
题目
给定一个字符串s,将s分割成一些子串,使每个子串都是回文。
返回s符合要求的的最少分割次数。样例
比如,给出字符串s = “aab”,
返回 1, 因为进行一次分割可以将字符串s分割成[“aa”,”b”]这样两个回文子串题解
1.可以借用136.Palindrome Partitioning-分割回文串(中等题)的方法,在查找所有分割方法的时候记录最少分割次数,但是会超时,代码就不贴了。
2.动态规划
public class Solution { /** * @param s a string * @return an integer */ private int min = Integer.MAX_VALUE; public int minCut(String s) { int len = s.length(); boolean P[][] = new boolean[len][len]; int dp[] = new int[len + 1]; for (int i = 0; i <= len; ++i) { dp[i] = len - i - 1; } for (int i = 0; i < len; ++i) { for (int j = 0; j < len; ++j) { P[i][j] = false; } } for (int i = len - 1; i >= 0; --i) { for (int j = i; j < len; ++j) { if (s.charAt(i) == s.charAt(j) && (j-i <= 1 || P[i + 1][j - 1])) { P[i][j] = true; dp[i] = Math.min(dp[i], dp[j + 1] + 1); } } } return dp[0]; }};
Last Update 2016.10.12
0 0
- 108.Palindrome Partitioning II-分割回文串 II(中等题)
- 136.Palindrome Partitioning-分割回文串(中等题)
- 最小回文分割数 Palindrome Partitioning II
- Palindrome Partitioning&II(回文字符串)
- 动态规划——回文最小分割数(palindrome-partitioning-ii)
- 分割成回文需要的最小分割数 Palindrome Partitioning II @LeetCode
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- LeetCode 132. Palindrome Partitioning II(回文切分)
- Palindrome Partitioning II 最小的括号分割
- Palindrome Partitioning II(找给定字符串分割次数获取回文字串, 动态规划)
- LeetCode | Palindrome Partitioning II(最少切割次数将一个子串划分成回文子串)
- 分割回文串II
- 分割回文串 II
- [Lintcode]Palindrome Partitioning分割回文串
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- Palindrome Partitioning II
- C++操作mysql方法总结(3)
- [转]响应式和自适应的区别
- AS之gradle的理解一(入门)
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- Android AutoLayout全新的适配方式 堪称适配终结者
- 108.Palindrome Partitioning II-分割回文串 II(中等题)
- 以 java 为例,总结下 appium 里的一方法的使用心得
- I-P-B frame简介
- 图片的处理-改尺寸、显圆形、判断图片类型
- Win7下配置"JAVA环境变量"---JDK的安装与配置
- 表面着色器附加顶点着色器片段 实现模型顶点动画
- kafka对比ActiveMQ
- ~Linux面试题汇总答案
- 还只知道“蓝瘦香菇”?来这里五分钟速成广西普通话十级