【LeetCode-131】Palindrome Partitioning
来源:互联网 发布:html怎么导入php文件 编辑:程序博客网 时间:2024/05/15 12:41
这道题有些难度,一步一步调试了半天才看明白递归的过程
public class PalindromePartitioning {//生成标志回文字符串的数组,partitioning_map[i][j] = 1的话,表明:string[i..j]是一个回文字符串//如果s.charAt(i) == s.charAt(j),partitioning_map[i + 1][j - 1] = 1,则partitioning_map[i][j] = 1//得到的这个dp[][]是一个上三角阵,下三角都是0,对角线都是1 public void dp(String s, byte[][] palindrome_map) { for(int i = s.length() - 1;i >= 0;i --) { for(int j = i;j < s.length();j ++) { if(i == j) { palindrome_map[i][j] = 1; } else { if(s.charAt(i) == s.charAt(j)) { if(j == i + 1 || palindrome_map[i + 1][j - 1]== 1) { palindrome_map[i][j] = 1; } } } } } } //根据生成好的回文标记数组对字符串进行划分 public void dfs(String s, int begin, byte[][] palindrome_map, ArrayList<String> array, ArrayList<ArrayList<String>> result) { if(begin == s.length()) { result.add(array); return; } for(int i = begin;i < s.length();i ++) { if(palindrome_map[begin][i] == 1) { ArrayList<String> tmp = new ArrayList<String>(array); tmp.add(s.substring(begin,i + 1)); dfs(s,i + 1,palindrome_map,tmp,result); } } } public ArrayList<ArrayList<String>> partition(String s) { ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>(); ArrayList<String> array = new ArrayList<String>(); if(s == null || s.length() == 0) { result.add(array); return result; } byte[][] palindrome_map = new byte[s.length()][s.length()]; dp(s, palindrome_map); dfs(s,0,palindrome_map,array,result); return result; }
0 0
- LeetCode: Palindrome Partitioning [131]
- LeetCode 131 Palindrome Partitioning
- LeetCode(131)Palindrome Partitioning
- [leetcode 131] Palindrome Partitioning
- [leetcode]131 Palindrome Partitioning
- LeetCode 131 Palindrome Partitioning
- leetcode ||131、Palindrome Partitioning
- LeetCode(131) Palindrome Partitioning
- leetcode 131: Palindrome Partitioning
- Leetcode #131 Palindrome Partitioning
- 131 Palindrome Partitioning [Leetcode]
- LeetCode 131 Palindrome Partitioning
- Leetcode 131:Palindrome Partitioning
- 【LeetCode-131】Palindrome Partitioning
- Leetcode 131 Palindrome Partitioning
- [leetCode 131]Palindrome Partitioning
- Leetcode 131 Palindrome Partitioning
- LeetCode 131 Palindrome Partitioning
- Linux-22、23-配置SecureCRT SSH客户端重要实用功能(L004-04,05)
- 从字符串中找出连续最长数字串
- 如何在activity中控制屏幕点亮与关闭
- android之ListView分页加载数据
- 数字比较的问题 short integer和数字比较
- 【LeetCode-131】Palindrome Partitioning
- ListView实现下拉刷新
- mysql 优化 整体思路
- C经典 指针函数 打印星期几
- 【深入理解JVM】:内存分配与回收策略
- 【DirectX11】第二篇 DirectX11渲染管线(2016.05.09更新)
- 深入学习Fragment
- njust 1925 sequence 拆分序列,寻找非降序列的最小个数,不变序列顺序。
- 实验二:ARM指令