动态规划——Palindrome Partitioning II
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
dp[i] = min(dp[j-1]+1),当0<j<=i时,并且s[j..i]是回文时
dp[i] = 0 ,当j=0,并且s[j..i]是回文时
在具体的dp数组递推运算过程中,需要这两个分支,同时会更新pa[j][i]的值便于后面的过程中回文条件的判断。
1 public int minCut(String s) { 2 int slen = s.length(); 3 int[][]pa = new int[slen][slen]; 4 int[]dp = new int[slen]; 5 for(int i = 0;i<slen;i++) 6 dp[i] = i; 7 for(int i = 0;i<slen;i++) 8 Arrays.fill(pa[i],0); 9 for(int i = 1;i<slen;i++) { 10 for(int j = 0;j<=i;j++) { 11 if(s.charAt(j)==s.charAt(i)&&((i-j<2)||pa[j+1][i-1]==1)) { 12 pa[j][i] = 1; 13 if(j!=0)dp[i] = Math.min(dp[i],dp[j-1]+1); 14 else dp[i] = 0; 15 } 16 } 17 } 18 return dp[slen-1]; 19 }
转载于:https://www.cnblogs.com/messi2017/p/9904220.html
动态规划——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 II
原题链接在这里:https://leetcode.com/problems/palindrome-partitioning-ii/ 题目: Given a string s, partition s ...
- LeetCode | Palindrome Partitioning I,II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Lintcode108 Palindrome Partitioning || solution 题解
[题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- Palindrome Partitioning
题目:Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- C++palindrome partitioning回文分割算法的实现(附完整源码)
C++palindrome partitioning回文分割算法的实现 C++palindrome partitioning回文分割算法的实现的完整源码(定义,实现,main函数测试) C++pali ...
- [Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning
136. Palindrome Partitioning / 131. Palindrome Partitioning 本题难度: Medium Topic: Search DFS Descripti ...
- [Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
最新文章
- 数据恢复工具PhotoRec
- 安装汇编环境,写一个最简单的窗口程序
- 《研磨设计模式》chap18 状态模式state(4)例子
- 初始Spring boot和一个入门SpringBoot工程
- 为SSIS编写自定义数据流组件(DataFlow Component)之进阶篇:自定义编辑器
- aptana studio 汉化与安装 zencoding、spket 配置
- spring 启动完成后事件监听器处理
- (84)JTAG接口与格雷码特点-面试必问(八)(第17天)
- gpg 中标麒麟获取 密钥失败_PHP实现码云Gitee的WebHook密钥验证算法
- Spark DF:关于Row中的数值获取问题
- Linux - wxWidgets安装和编译HelloWorld
- Cesium教程系列汇总
- GJB 软件质量保证计划(模板)
- 质点碰撞和卢瑟福公式
- 信息安全竞赛优秀作品介绍1
- 树莓派4B WIFI 物理网口设置固定IP方法
- TrafficStats 网络实时测速
- 最新H5网页分享到Twitter、Facebook带缩略图
- 一手源刊|10月SCI/SSCI/EI刊源已更新, 新增多本TOP/CCF-B优质刊~
- 小程序原生swiper中bindtransition监听滑动效果