[LeetCode]131 Palindrome Partitioning
2015-01-08 14:55
411 查看
https://oj.leetcode.com/problems/palindrome-partitioning/
http://blog.csdn.net/linhuanmars/article/details/22777711
http://blog.csdn.net/linhuanmars/article/details/22777711
public class Solution { public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); if (s == null || s.isEmpty()) return result; boolean[][] dict = buildDict(s); help(s, dict, 0, new ArrayList<String>(), result); return result; } private void help(String s, boolean[][] dict, int start, List<String> item, List<List<String>> result) { if (start >= s.length()) { result.add(new ArrayList<String>(item)); return; } for (int i = start; i < s.length() ; i ++) { if (!dict[start][i]) continue; item.add(s.substring(start, i + 1)); help(s, dict, i + 1, item, result); item.remove(item.size() - 1); } } // dict[i][j] means substring(i, j + 1) is palin // dict(i, j) = char[i] == char[j] && dict(i + 1, j - 1) // So i starts from s.length to 0 // j starts from i to s.length private boolean[][] buildDict(String s) { int len = s.length(); boolean[][] dict = new boolean[len][len]; for (int i = len - 1 ; i >= 0 ; i --) { for (int j = i ; j < len ; j ++) { if (i == j) dict[i][j] = true; else if(i + 1 == j) dict[i][j] = s.charAt(i) == s.charAt(j); else dict[i][j] = s.charAt(i) == s.charAt(j) && dict[i + 1][j - 1]; } } return dict; } }
相关文章推荐
- Leetcode 131:Palindrome Partitioning
- LeetCode 131: Palindrome Partition
- leetcode——131——Palindrome Partitioning
- leetcode 131: Palindrome Partitioning
- [Leetcode 131, Medium] Palindrome Partitioning
- Leetcode 131 Palindrome Partitioning 回文分割
- 131. Palindrome Partitioning LeetCode
- LeetCode 131 Palindrome Partitioning
- [LeetCode 131] - 回文分割(Palindrome Partitioning)
- Leetcode no. 131
- Leetcode 131 Palindrome Partitioning
- [leetcode-131]Palindrome Partitioning(java)
- [leetcode 131] Palindrome Partitioning
- LeetCode 131 Palindrome Partitioning
- leetcode做题总结,题目Palindrome Partitioning 131
- leetcode 131|132. Palindrome Partitioning 1|2
- [leetcode]131 Palindrome Partitioning
- LeetCode 131 Palindrome Partitioning (DFS)
- LeetCode 2015.8.6 17,216,131,215,230,34,92
- leetcode[131]Palindrome Partitioning