LeetCode 132. Palindrome Partitioning II(回文切分)
2016-05-27 00:22
507 查看
原题网址: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 =
Return
be produced using 1 cut.
方法:动态规划。
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
1since the palindrome partitioning
["aa","b"]could
be produced using 1 cut.
方法:动态规划。
public class Solution { public int minCut(String s) { char[] sarray = s.toCharArray(); int n = s.length(); int[] mins = new int[n+1]; Arrays.fill(mins, Integer.MAX_VALUE); mins[0] = 0; for(int i=0; i<n; i++) { mins[i+1] = Math.min(mins[i+1], mins[i] + 1); for(int j=1; i-j>=0 && i+j<=n-1; j++) { if (sarray[i-j] != sarray[i+j]) break; mins[i+j+1] = Math.min(mins[i+j+1], mins[i-j]+1); } for(int j=0; i-j>=0 && i+j+1<=n-1; j++) { if (sarray[i-j] != sarray[i+j+1]) break; mins[i+j+2] = Math.min(mins[i+j+2], mins[i-j]+1); } } return mins -1; } }
相关文章推荐
- LeetCode 133. Clone Graph(克隆图)
- 原型链(__proto__)
- makefile之VPATH和vpath的使用
- System.in.read()
- javascript的 Object 和 Function
- 快速多人游戏(1) - 介绍
- 概率世界
- 如何配置php运行环境
- Share Ubuntu Home Directories using Samba
- php性能测试工具--xhprof
- linux篇——centOs下的vim配置
- dll 子类化 记事本 相应自己的功能
- 用NSMutableAttributedString快速设置UIbutton Title/lable的文字:字体,大小,颜色
- 拦截器getmodel方法什么时候被调用(没搞懂有什么鸟用,自己搭的项目中用到了这个)
- Convention插件的使用(会涉及content目录,jsp必须放入这个下面才能映射成功基于注解的配置)
- Android 动画目录-概要
- 最新版SDWebImage的使用
- Spring组件扫描<context:component-scan/>使用详解
- css 加号 选择器
- Eclipse从数据库逆向生成Hibernate带注解的实体类