- 浏览: 173533 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 334Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 512Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 430Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 620Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 519Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 533Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 736You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
C#,回文分割问题(Palindrome Partitioning Problem)算法与源代码 1 回文串 “回文串”是一个正读和反读都一样的字符串,初始化标志flag=true,比如“level”或者“noon”等等就是回文串。 2 回文分割问题 给定...
Palindrome Partitioning II Maximal Rectangle Best Time to Buy and Sell Stock III Best Time to Buy and Sell Stock IV Best Time to Buy and Sell Stock with Cooldown Interleaving String Scramble String ...
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
Determine whether an integer is a palindrome. Do this without extra space. Java AC版本
北大POJ1159-Palindrome 解题报告+AC代码
C++实现的Palindrome,回文 C++实现的Palindrome,回文
Pku acm 第1159题 Palindrome 代码,有详细的注释,动态规划
各位帮帮忙吧
回文分区 动态规划 | 第 17 组(回文分区)( )
Palindrome Sub-Array,Problem Description A palindrome sequence is a sequence which is as same as its reversed order. For example, 1 2 3 2 1 is a palindrome sequence, but 1 2 3 2 2 is not. Given a 2-...
palindrome22.in
回文数Java
北大POJ1159-Palindrome
LeetCode Palindrome Number解决方案
palindrome_prime.c
Palindrome.py
检查字符串是否为palindrome, 从前后分别检查,并计算出相同或不同的数量。
GET /java-palindrome-example/palindrome/ 解析提供的字符串,并找到其中包含的最大回文。 在此情况下,也可以将type的可选查询参数设置为slow在这种情况下,服务将使用慢得多的递归算法。 例子 GET /java-...