- 浏览: 183544 次
- 性别:
- 来自: 济南
文章分类
最新评论
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"]
]
给定一个字符串,让我们把字符串分割成回文子串,并把所有可能的情况输出。我们采用回溯法,在for循环中递归搜索,如果当然搜索的子串是回文字符串,就把它加入到list中。代码如下:
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
]
给定一个字符串,让我们把字符串分割成回文子串,并把所有可能的情况输出。我们采用回溯法,在for循环中递归搜索,如果当然搜索的子串是回文字符串,就把它加入到list中。代码如下:
public class Solution { public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<List<String>>(); LinkedList<String> list = new LinkedList<String>(); if(s == null) return result; getPartition(0, s, list, result); return result; } public void getPartition(int start, String s, LinkedList<String> list, List<List<String>> result) { if(start == s.length()) { result.add(new LinkedList<String>(list)); } for(int i = start; i < s.length(); i++) { if(isPalindrome(s.substring(start, i + 1))) { list.add(s.substring(start, i + 1)); getPartition(i + 1, s, list, result); list.removeLast(); } } } public boolean isPalindrome(String s) { int l = 0; int r = s.length() - 1; while(l < r) { if(s.charAt(l) != s.charAt(r)) return false; l ++; r --; } return true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
C#,回文分割问题(Palindrome Partitioning Problem)算法与源代码 1 回文串 “回文串”是一个正读和反读都一样的字符串,初始化标志flag=true,比如“level”或者“noon”等等就是回文串。 2 回文分割问题 给定...
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项目中,每个问题的解决方案都提供了清晰的思路和...