LeetCode Palindrome Partitioning II
原题链接在这里:https://leetcode.com/problems/palindrome-partitioning-ii/
题目:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
题解:
与Word Break相似. DP问题, 求的是至少切几刀能全是palindrome. 需要保留的历史信息是到当前点 最少 能分成几块palindrome, 用array保存.
更新时如果[i,j]段是palindrome, 到j点结束那段就可以从 到i点结束那段+1 来跟新最小值.
初始化至少每个字母都分开肯定都是palindrome了.
答案dp[len]-1. 最少分成的块数 比切的刀数 多一.
用二维array来村[i, j]段是否是palindrome. 两边闭区间,包括i,j对应的char.
Time Complexity: O(len^2). Space: O(len^2).
AC Java:
1 public class Solution { 2 public int minCut(String s) { 3 if(s == null || s.length() == 0){ 4 return 0; 5 } 6 int len = s.length(); 7 boolean[][] isDic = helper(s); 8 int [] res = new int[len+1]; 9 res[0] = 0; 10 for(int i = 0; i < len; i++){ 11 res[i+1] = i+1; 12 for(int j = 0; j <= i; j++){ //error 13 if(isDic[j][i]){ 14 res[i+1] = Math.min(res[i+1],res[j]+1); 15 } 16 } 17 } 18 return res[len] - 1; 19 } 20 21 //helper function builds dictionary 22 private boolean[][] helper(String s){ 23 int len = s.length(); 24 boolean[][] dict = new boolean[len][len]; 25 for(int i = len-1; i>=0; i--){ 26 for(int j = i; j < len; j++){ 27 if(s.charAt(i) == s.charAt(j) && ((j-i)<2 || dict[i+1][j-1] )){ //error 28 dict[i][j] = true; 29 } 30 } 31 } 32 return dict; 33 } 34 }
可以省略掉dic. 对每一个点按照奇数和偶数两种方式 左右延展若是palindrome就更新dp array. 若不是就停止.
Time Complexity: O(len^2). Space: O(len).
AC Java:
1 class Solution { 2 public int minCut(String s) { 3 if(s == null || s.length() == 0){ 4 return 0; 5 } 6 7 int len = s.length(); 8 int [] dp = new int[len+1]; 9 for(int i = 0; i<=len; i++){ 10 dp[i] = i-1; 11 } 12 for(int i = 0; i<len; i++){ 13 // odd length palindrome 14 for(int l = i, r = i; l>=0 && r<len && s.charAt(l)==s.charAt(r); l--, r++){ 15 dp[r+1] = Math.min(dp[r+1], dp[l]+1); 16 } 17 18 // even length palindrome 19 for(int l = i, r = i+1; l>=0 && r<len && s.charAt(l)==s.charAt(r); l--, r++){ 20 dp[r+1] = Math.min(dp[r+1], dp[l]+1); 21 } 22 } 23 return dp[len]; 24 } 25 }
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4824961.html
LeetCode Palindrome Partitioning II相关推荐
- 【回文串5 重点+动态规划】LeetCode 132. Palindrome Partitioning II
LeetCode 132. Palindrome Partitioning II Solution1:我的答案1 直接模仿131那道题的DFS解法,找其中size最小的.果不其然,因为超时只能部分AC ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- LeetCode | Palindrome Partitioning I,II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode Palindrome Partitioning(dfs +回文串 )
问题:给出一个字符串,输出回文串,使得拼接后为原来的字符串 思路:先通过动态规划得到所有的回文串,然后使用深度优先搜索得到所有的解 具体代码参考: https://github.com/wuli249 ...
- [LeetCode]Palindrome Partitioning 找出所有可能的组合回文
给定一个字符串,切割字符串,这样每个子字符串是一个回文字符串. 要找出所有可能的组合. 办法:暴力搜索+回溯 class Solution { public:int *b,n;vector<ve ...
- [Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
- 【回文串4 DFS】LeetCode 131. Palindrome Partitioning
LeetCode 131. Palindrome Partitioning DFS的经典套路题目!!! 八皇后问题写法类似!!! Solution1: 转载网址:http://www.cnblogs. ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- [Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning
136. Palindrome Partitioning / 131. Palindrome Partitioning 本题难度: Medium Topic: Search DFS Descripti ...
最新文章
- ZABBIX企业微信新版告警
- 二叉树的遍历(包括递归和非递归方法)
- PHP从入门到跑路(二),基础语法,数据库操作
- 【QM-03】Dynamic Modification Rule (动态修改规则)
- OpenCV:OpenCV图像旋转的代码
- Guacamole-RDP没有声音解决办法
- 清新手绘水果平面设计|面膜的包装设计越来越精致了!
- [CoffeeScript]使用Yield功能
- 《21天学通HTML+CSS+JavaScript Web开发(第7版)》——2.6 Web托管
- inno setup安装之前关闭mysql_inno setup 安装前判断进程是否存在,以及停止相应进程转...
- anaconda + tensorflow +ubuntu 超级菜鸟,大家多指正【转】
- eagle8.6 linux 补丁,Ubuntu 12.04无法安装Eagle-6.x 解决方法
- html不支持js,解决Firefox不支持Js的InnerHtml问题
- QQ配置文件解密(转)
- Flash总弹出广告怎么办?该如何删除?
- Android各版本兼容性适配
- 计算机网络-自顶向下方法-笔记【第3章-传输层】
- cmd如何打开、运行?
- 常用的java日期处理
- 网站被流量攻击怎么处理
热门文章
- python如何用色度表示数值大小_python中色度通道的YUV子采样
- 【分享-快速仿站】无敌超强仿站小工具一键下载
- 如何查看服务器gpu性能,ubuntu服务器查看GPU和CPU实时使用情况
- Vue笔记-vue3中.en.dev文件及axios.defaults.baseURL的使用
- Linux工作笔记-查看某程序安装路径及可执行文件相关链接库
- MySQL工作笔记-解决导入外部sql中文乱码问题
- Qt工作笔记-QTreeWidget求总结点数以及此树中最多孩子的个数(非递归)
- C/C++如何连接MySQL服务器以及简单加密
- 5.3矩阵的压缩存储(稀疏矩阵转置和快速转置)
- 宁夏小学三年级计算机下册教案,宁夏信息技术教案