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"] ]
public class Solution { public List<List<String>> partition(String s) { List<String> item = new ArrayList<String>(); List<List<String>> res = new ArrayList<List<String>>(); if (s == null || s.length() == 0) { return res; } dfs(s,0,item,res); return res; } private void dfs(String s, int start, List<String> item, List<List<String>> res) { // TODO Auto-generated method stub if (start == s.length()) { res.add(new ArrayList<String>(item)); return; } for (int i = start; i < s.length(); i++) { String str = s.substring(start, i+1); if (isPalindrome(str)) { item.add(str); dfs(s, i+1, item, res); item.remove(item.size() - 1); } } } private boolean isPalindrome(String s) { int low = 0; int high = s.length()-1; while(low < high){ if(s.charAt(low) != s.charAt(high)) { return false; } low++; high--; } return true; } }
相关推荐
python python_leetcode题解之131_Palindrome_Partitioning
javascript js_leetcode题解之131-palindrome-partitioning.js
python python_leetcode题解之132_Palindrome_Partitioning_II
javascript js_leetcode题解之132-palindrome-partitioning-ii.js
1. 项目源代码:可能有一个名为"PalindromePartitioning.java"的文件,其中包含了用Java实现的回文分区算法。 2. 测试用例:可能有测试文件用于验证算法的正确性,比如"TestPalindromePartitioning.java"。 3. 解释...
Maximum Subarray, 删除重复的排序数组, 删除重复的排序数组 2, 查找没有重复的最长子串, 包含两个独特字符的最长子串, Palindrome Partitioning 这些高级算法和经典问题是程序员在代码面试中经常遇到的问题,了解...
1. **Palindrome Partitioning (mine).cpp** - 这个文件是关于LeetCode的第131题“回文分割”。该问题要求将一个字符串分割成多个回文子串。解冑通常涉及深度优先搜索或动态规划,寻找所有可能的分割方案并检查是否...
leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 ...Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
partitioning - regex - sudoku solver: 37 排序 - merge sort - quick sort - insertion sort - selection sort - counting sort 位操作 - find the only element which exists once/twice... - count
leetcode力扣是什么 leetcode-按类别 看了一些leetcode刷题指南,总结一下两个重点,一是最好按照类别刷题,总结思路;...Partitioning (hard) 212 Word Search II (hard) DFS /二叉树 the difference between df
3. 题目131 - "Palindrome Partitioning" 这是一道回文子串分割问题,要求将一个字符串分割成多个回文子串。常见的解法是动态规划,创建一个二维数组来存储子问题的最优解。在Java中,可以使用StringBuilder和...
分割回文串(Palindrome Partitioning)**:这是一个关于字符串处理的题目,要求将一个字符串分割成若干个回文子串。主要涉及的算法是深度优先搜索(DFS),通过递归的方式检查所有可能的分割方案,判断是否都是回文。 ...
Palindrome_Partitioning DP,避免递归调用,利用数组储存已算出来的数值,由后向前逐渐增加字符串处理 Palindrome_Partitioning_2 同上 Sum_Root_to_Leaf_Numbers DFS Surrounded_Regions 由四周‘O’开始向内检索...
gas station leetcode 在牛客网上的在线编程...palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
对于更高级的题目,可能会涉及图论或动态规划,比如`0131_palindrome_partitioning.py`可能涉及回文子串分割,这通常会用到动态规划来避免重复计算。 在PythonAlgo项目中,每个问题的解决方案都提供了清晰的思路和...