码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Palindrome Partitioning II

时间:2014-12-15 21:50:08      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:dp   dynamic programming   动态规划   

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.

先DP找出所有的子回文字符串存储在f中,然后用数组h记录某位置最少的分割数,也是DP


public class Solution {
	boolean f[][];
	int res = Integer.MAX_VALUE;
	int h[];
	public int minCut(String s) {
		f = new boolean[s.length()][s.length()];
		for (int i = 0; i < s.length(); i++) {
			f[i][i] = true;
		}
		int maxLen = 1;
		for (int i = 0; i < s.length() - 1; i++) {
			if (s.charAt(i) == s.charAt(i + 1)) {
				f[i][i + 1] = true;
				maxLen = 2;
			}
		}
		for (int len = 3; len <= s.length(); len++) {
			boolean in = false;
			for (int i = 0; i < s.length() - len + 1; i++) {
				if (s.charAt(i) == s.charAt(i + len - 1)
						&& f[i + 1][i + len - 2]) {
					f[i][i + len - 1] = true;
					in = true;
				}
			}
			if (in) {
				maxLen = len;
			}
		}
		if (maxLen == s.length())
			return 0;
		h = new int[s.length()+1];
		h[0] = -1;
		for(int i=1;i<=s.length();i++){
			h[i] = Integer.MAX_VALUE;
			for(int j=1;j<=i;j++){
				if(f[j-1][i-1]&&h[j-1]!=Integer.MAX_VALUE){
					h[i] = Math.min(h[i], h[j-1]+1);
				}
			}
		}
		return h[s.length()];
	}
}


[LeetCode]Palindrome Partitioning II

标签:dp   dynamic programming   动态规划   

原文地址:http://blog.csdn.net/guorudi/article/details/41948989

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!