leetcode Palindrome Partitioning
来源:互联网 发布:二手警服淘宝 编辑:程序博客网 时间:2024/06/05 08:20
Palindrome Partitioning 原题地址:
https://oj.leetcode.com/problems/palindrome-partitioning/
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"] ]之前有道动态规划的题,加一个dfs就可以。
public class Solution { private List<List<String>> plist = new ArrayList<List<String>>();public List<List<String>> partition(String s) {if (s == null || s.length() == 0)return plist;int len = s.length();boolean[][] palin = new boolean[len][len];for (int h = 0; h < len; h++)for (int i = 0; i < len-h; i++) {if (s.charAt(i) == s.charAt(i+h) && (h < 2 || palin[i+1][i+h-1]))palin[i][i+h] = true;}LinkedList<String> list = new LinkedList<String>();dfs(palin, list, s, 0);return plist;}private void dfs(boolean[][] palin, LinkedList<String> list, String s, int v) {if (v == s.length()) {List<String> _list = (List<String>) list.clone();plist.add(_list);return;}for (int w = v; w < s.length(); w++) {if (palin[v][w]) {String temp = s.substring(v, w+1);list.add(temp);dfs(palin, list, s, w+1);list.pollLast();}}}}
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- 阿里负责人揭秘面试潜规则
- SQLPLUS中应用参数查询
- poj 1017 packets 装箱问题
- HTML学习笔记(七)
- OpenGL 超级宝典学习,制作 圆柱,球体,花托,圆盘等多边体
- leetcode Palindrome Partitioning
- 黑马程序员——静态代理与动态代理
- 经典玩笑
- 机器学习进阶之贝叶斯
- Codeforces Round #277 (Div. 2)(D)
- Android开发之画笔
- 央视2014“杰出工程师奖”颁奖典礼将播出 致敬工程师
- 让爱继续
- leetcode Letter Combinations of a Phone Number