CODE 1: Palindrome Partitioning II

原创 2013年09月08日 15:08:02

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.

 

	/**
	 * 使用动态规划计算任意两点间是否为回文
	 * @param s 字符串
	 * @return  最小Cut次数
	 */
	private int partition(String s) {
		if (s == null || s.length() == 0)
			return 0;
		
		boolean[][] isPalindromes = new boolean[s.length()][s.length()]; // 表示任意两点之间的字符串是否为回文

		for (int i = 0; i < s.length(); i++) {
			isPalindromes[i][i] = true;
		}
		for (int i = s.length() - 2; i >= 0; i--) {
			isPalindromes[i][i + 1] = s.charAt(i) == s.charAt(i + 1);
			for (int j = i + 2; j < s.length(); j++)
				isPalindromes[i][j] = (s.charAt(i) == s.charAt(j)) && isPalindromes[i + 1][j - 1];
		}

		return getMinCut(s, isPalindromes);
	}

	/**
	 * 使用动态规划计算最小Cut次数
	 * @param s 字符串
	 * @param isPalindromes 表示任意两点之间的字符串是否为回文
	 * @return
	 */
	private int getMinCut(String s, boolean[][] isPalindromes) {
		int[] cuts = new int[s.length()];
		for (int i = s.length() - 1; i >= 0; i--) {
			if (isPalindromes[i][s.length() - 1]) {
				cuts[i] = 0;
				continue;
			}
			int min = Integer.MAX_VALUE;
			for (int j = s.length() - 2; j >= i; j--) {
				if (isPalindromes[i][j]) {
					int tmp = 1 + cuts[j + 1];
					min = min > tmp ? tmp : min;
				}
			}
			cuts[i] = min;
		}
		return cuts[0];
	}


 

Microsoft Agent技术在Delphi中的应用

解放军合肥炮兵学院 张建军 中保财险安徽省铜陵市分公司 韩莹 ---- Microsoft Agent是微软公司于1997年9月发布的一项代理软件开发技术,而后被集成到Internet Explore...
  • deardelphi
  • deardelphi
  • 2000-08-26 10:06:00
  • 689

leetcode之 Palindrome Partitioning I&II

1 Palindrome Partitioning 问题来源:PalindromePartitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有...
  • yutianzuijin
  • yutianzuijin
  • 2013-11-20 21:06:00
  • 13915

【LeetCode】132. Palindrome Partitioning II 基于动态规划DP、C++、Java的分析及解法

132. Palindrome Partitioning II Total Accepted: 50256 Total Submissions: 230441 Difficulty: Hard ...
  • Jin_Kwok
  • Jin_Kwok
  • 2016-05-16 20:29:43
  • 920

LeetCode 132 Palindrome Partitioning II (动态规划)

LeetCode 132 Palindrome Partitioning II (动态规划)
  • Tc_To_Top
  • Tc_To_Top
  • 2016-10-23 11:59:51
  • 765

LeetCode 132. Palindrome Partitioning II(回文切分)

原题网址:https://leetcode.com/problems/palindrome-partitioning-ii/ Given a string s, partition s such ...
  • jmspan
  • jmspan
  • 2016-05-27 00:22:56
  • 372

Leetcode(132) Palindrome Partitioning II

题目如下: Given a string s, partition s such that every substring of the partition is a palindrome. Retu...
  • feliciafay
  • feliciafay
  • 2014-12-17 03:47:14
  • 1079

Palindrome Partitioning -- LeetCode

原题链接: http://oj.leetcode.com/problems/palindrome-partitioning/  这道题是求一个字符串中回文子串的切割,并且输出切割结果,其实是Word ...
  • linhuanmars
  • linhuanmars
  • 2014-04-02 08:44:17
  • 11799

Leetocde: Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return th...
  • doc_sgl
  • doc_sgl
  • 2013-10-28 23:41:31
  • 6352

#108 Palindrome Partitioning II

题目描述: Given a string s, cut s into some substrings such that every substring is a palindrome....
  • haifischxia
  • haifischxia
  • 2016-08-25 14:39:18
  • 94

Palindrome Partitioning II Leetcode Python

Given a string s, partition s such that every substring of the partition is a palindrome. Retur...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-25 02:24:16
  • 302
收藏助手
不良信息举报
您举报文章:CODE 1: Palindrome Partitioning II
举报原因:
原因补充:

(最多只允许输入30个字)