LeetCode: Palindrome Partitioning 解题报告
2014-10-20 16:51
288 查看
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
[b]Solution 0:[/b]
直接用DFS 做,实际上也是可以过Leetcode的检查的。
View Code
Runtime: 524 ms, 实际上运行时间也没多少改善。可能是数据集大小的问题咯。
[b]GitHub Link:[/b]
Partition.java
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dfs/Partition_2014_1229.java
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
[b]Solution 0:[/b]
直接用DFS 做,实际上也是可以过Leetcode的检查的。
// BUG 3: use boolean instead of Boolean. public static boolean isPalindromeHash(String s, HashMap<String, Boolean> map) { int len = s.length(); int left = 0; int right = len - 1; if (map.get(s) != null) { return map.get(s); } map.put(s, true); while (left < right) { if (s.charAt(left) != s.charAt(right)) { map.put(s, false); return false; } left++; right--; } return true; } // Solution 3: Use DP to determine the palindrome first. public List<List<String>> partition(String s) { List<List<String>> ret = new ArrayList<List<String>>(); if (s == null) { return ret; } int len = s.length(); // D[i][j]: if this a palindrom for s.substring(i, j + 1). boolean[][] D = new boolean[len][len]; for (int j = 0; j < len; j++) { for (int i = 0; i <= j; i++) { D[i][j] = s.charAt(i) == s.charAt(j) && (j - i <= 2 || D[i + 1][j - 1]); } } // bug: new map error. dfs3(s, 0, new ArrayList<String>(), ret, D); return ret; } public static void dfs3(String s, int index, List<String> path, List<List<String>> ret, boolean[][] D) { int len = s.length(); if (index == len) { ret.add(new ArrayList<String>(path)); return; } for (int i = index; i < len; i++) { String sub = s.substring(index, i + 1); if (!D[index][i]) { continue; } path.add(sub); dfs3(s, i + 1, path, ret, D); path.remove(path.size() - 1); } }
View Code
Runtime: 524 ms, 实际上运行时间也没多少改善。可能是数据集大小的问题咯。
[b]GitHub Link:[/b]
Partition.java
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dfs/Partition_2014_1229.java
相关文章推荐
- [LeetCode] 269. Alien Dictionary 解题报告
- [Leetcode] 591. Tag Validator 解题报告
- Leetcode: Remove Duplicates from Sorted List II 解题报告
- [Leetcode] 369. Plus One Linked List 解题报告
- Leetcode 119. Pascal's Triangle II 杨辉三角2 解题报告
- [Leetcode] 737. Sentence Similarity II 解题报告
- 【LeetCode】Reverse String II 解题报告
- [Leetcode] 280. Wiggle Sort 解题报告
- [LeetCode] Maximum Subarray 解题报告
- [LeetCode]Unique Binary Search Trees, 解题报告
- [leetcode] 74. Search a 2D Matrix 解题报告
- [Leetcode] 616. Add Bold Tag in String 解题报告
- 【LeetCode】3Sum Closest 解题报告
- [leetcode] 153. Find Minimum in Rotated Sorted Array 解题报告
- LeetCode 解题报告 Max Points on a Line
- 【LeetCode】667. Beautiful Arrangement II 解题报告(Python)
- [LeetCode] Simplify Path 解题报告
- Leetcode #40. Combination Sum II 组合求和2 解题报告
- Leetcode 417. Pacific Atlantic Water Flow 流向海洋 解题报告
- leetcode解题报告233——Power of Two