leetcode - Palindrome Partitioning II
2013-04-19 17:32
120 查看
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
动态规划:
记忆化搜索实现,能pass small data set,但运行large data set里 TLE了。
这里采用了记忆化搜索方法记录了字符串是否为回文串的信息,但依然TLE,看样子得想别的办法了,该算法改用递推法实现为:
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.
动态规划:
记忆化搜索实现,能pass small data set,但运行large data set里 TLE了。
public class Solution { private java.util.Map<String, Integer> map = new java.util.HashMap<String, Integer>(); private java.util.Map<String, Boolean> palinMap = new java.util.HashMap<String, Boolean>(); public int minCut(String s) { if (s.length() == 1) return 0; if (map.get(s) != null) return map.get(s); if (isPalindrome(s)) { map.put(s, 0); return 0; } int ret = s.length()-1; for (int i=1; i<s.length(); i++) { ret = Math.min(ret, minCut(s.substring(0, i))+1+minCut(s.substring(i))); } map.put(s, ret); return ret; } public boolean isPalindrome(String s) { if (palinMap.get(s) != null) return palinMap.get(s); boolean ret = true; int i=0, j=s.length()-1; while (i < j) { char b = s.charAt(i++); char e = s.charAt(j--); if (b != e) { ret = false; break; } } palinMap.put(s, ret); return ret; } }
这里采用了记忆化搜索方法记录了字符串是否为回文串的信息,但依然TLE,看样子得想别的办法了,该算法改用递推法实现为:
public class Solution { private java.util.Map<String, Boolean> palinMap = new java.util.HashMap<String, Boolean>(); public int minCut(String s) { int len = s.length(); int[][] data = new int[len][len]; for (int i=0; i<len; i++) for (int j=0; j<len; j++) if (i==j) data[i][j] = 0; else data[i][j] = s.length()-1; for (int add=1; add<len; add++) for (int i=0; i<len&&(i+add)<len; i++) { if (isPalindrome(s.substring(i, i+add+1))) { data[i][i+add] = 0; } else { for (int d=1; d<=add; d++) { data[i][i+add] = Math.min(data[i][i+add], data[i][i+d-1]+data[i+d][i+add]+1); } } } return data[0][len-1]; } public boolean isPalindrome(String s) { if (palinMap.get(s) != null) return palinMap.get(s); boolean ret = true; int i=0, j=s.length()-1; while (i < j) { char b = s.charAt(i++); char e = s.charAt(j--); if (b != e) { ret = false; break; } } palinMap.put(s, ret); return ret; } }
相关文章推荐
- Palindrome Partitioning II--LeetCode
- Palindrome Partitioning II -- leetcode
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- Leetcode237: Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- LeetCode_Palindrome Partitioning II
- LeetCode 132. Palindrome Partitioning II
- 【LeetCode】Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- Leetcode_palindrome-partitioning-ii
- Leetcode(132) Palindrome Partitioning II
- 132 Palindrome Partitioning II [Leetcode]
- leetcode(palindrome-partitioning-ii)
- [leetcode]132. Palindrome Partitioning II(Java)
- 【LeetCode】Palindrome Partitioning I&II
- [leetcode]Palindrome Partitioning II
- [LeetCode] 065: Palindrome Partitioning II
- LeetCode - Palindrome Partitioning II
- [leetcode][DP] Palindrome Partitioning II