标签:
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.
给定一个字符串s,将s分割成一些子串,使每个子串都是回文。
比如,给出字符串s = "aab",
返回 1, 因为进行一次分割可以将字符串s分割成["aa","b"]这样两个回文子串
public class Solution { public int minCut(String s) { int n = s.length(); int[] cuts = new int[n]; boolean[][] dp = new boolean[n][n]; for (int i = 0; i < n; i++) { cuts[i] = i; for (int j = 0; j <= i; j++) { if (s.charAt(i) == s.charAt(j) && (i - j <= 1 || dp[j + 1][i - 1])) { dp[j][i] = true; if (j > 0) { cuts[i] = Math.min(cuts[i], cuts[j - 1] + 1); } else { cuts[i] = 0; } } } } return cuts[n -1]; } }
Palindrome Partitioning II Leetcode
标签:
原文地址:http://www.cnblogs.com/iwangzheng/p/5798608.html