1. 程式人生 > >Palindrome Partitioning II

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 = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

public class Solution {
    public int minCut(String s) {
        int n = s.length();
		boolean [][] p = new boolean [n][n];
		int [] res = new int[n + 1];
		for (int i = 0; i < n + 1; i++) {
				res[i] = n-i-1;
		}
		for (int i = n - 1; i >= 0; i--) {
			for (int j = i  ; j < n; j++) {
				if (s.charAt(i) == s.charAt(j) && (j - i < 2 || p[i + 1][j - 1])) {
					p[i][j] = true;
					res[i] = min(res[i], res[j + 1] + 1);
				}
			}
		}
		return res[0];
    }
	private int min(int a, int b){
		return a < b ? a : b;
	}
}

也是DP,但只要用一維的結果就可以了。

相關推薦

[LintCode] Palindrome Partitioning II

com set list could private produced sin ray star Given a string s, cut s into some substrings such that every substring is a palindrome

132. Palindrome Partitioning II

rom urn [] for public char min () int class Solution { public int minCut(String s) { int[] dp=new int[s.length()+1];

分割回文串 II · Palindrome Partitioning II

class nbsp 英文 長度 總結 正常 bsp 風格 思路 [抄題]: 給定一個字符串s,將s分割成一些子串,使每個子串都是回文。 返回s符合要求的的最少分割次數。 [思維問題]: [一句話思路]: [輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常

132 Palindrome Partitioning II 分割回文串 II

回文 回文串 tco ++ ali 例如 post 字符 code 給定一個字符串 s,將 s 分割成一些子串,使每個子串都是回文串。返回 s 符合要求的的最少分割次數。例如,給出 s = "aab",返回 1 因為進行一次分割可以將字符串 s 分割成 ["aa","b"]

LeetCode 132. 分割回文串 IIPalindrome Partitioning II

題目描述   給定一個字串 s,將 s 分割成一些子串,使每個子串都是迴文串。 返回符合要求的最少分割次數。 示例: 輸入: "aab" 輸出: 1 解釋: 進行一次分割就可將 s 分割成 ["aa","b"] 這樣兩個迴文子串。 &nb

【LeetCode】121.Palindrome Partitioning II

題目描述(Hard) Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed

動態規劃——Palindrome Partitioning II

Palindrome Partitioning II 這個題意思挺好理解,提供一個字串s,將s分割成多個子串,這些字串都是迴文,要求輸出分割的最小次數。 Example: Input: "aab" Output: 1 Explanation: The palindrome parti

leetcode | 132. 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 p

python leetcode 132. Palindrome Partitioning II

思路:如果s[left:right]是迴文並且s[:left]也是迴文,那麼s[:right]即是一個分割。同理如果dp[left:right]是最小回文分割並且dp[:left]也是最小回文分割,那麼dp[:right]=dp[left:right]+dp[:left]。為了方便遍歷,這裡

LeetCode:132. Palindrome Partitioning II

題目是這樣: Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed fo

演算法作業第十週(leetcode)——132.palindrome-partitioning-ii

其實已經不是特別想做動態規劃的題了,但是還是抽到了一道動態規劃的題。下面給出題目描述: 這題的大意是給一個字串,要經過最少多少次分割才能把字串分成迴文串組成的字串。 其實這題動態規劃的部分並不是特別難的部分。很容易就可以想到用cut[i]來表示前i個字元所能分成的迴文

【LeetCode】#132分割回文串II(Palindrome Partitioning II)

【LeetCode】#132分割回文串II(Palindrome Partitioning II) 題目描述 給定一個字串 s,將 s 分割成一些子串,使每個子串都是迴文串。 返回符合要求的最少分割次數。 示例 輸入: “aab” 輸出: 1 解釋: 進行一次分割就可將 s

[LeetCode] 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 o

DP動態規劃專題六 :LeetCode 132. 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 partit

動態規劃——迴文最小分割數(palindrome-partitioning-ii

題目: 給定一個字串str,返回把str全部切成迴文子串的最小分割數。 舉例: str="ABA" ,不需要切割,返回0; str="ACDCDCDAD",最少需要切兩次,比如"A","CDCDC"

LeetCode : 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 palindr

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 palindro

LeetCode 132. 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 pal

迴文最小分割數(palindrome-partitioning-ii

class Solution { public: int minCut(string s) { //先求解小段的子序列 vector<vector<int>> dp(s

LeetCode Palindrome Partitioning II

這道題與相似。用DP來做,需要保留的歷史資訊就是到當前點能分成幾塊Palindrome, 用一個int陣列res保留。每次更新res[i+1], 比較i+1和res[j]+1大小,取小的。因為單個letter肯定是palindrome, 所以肯定會被字典拆開,但[j.