**Palindrome Partitioning

2024-06-18 17:48
文章标签 partitioning palindrome

本文主要是介绍**Palindrome Partitioning,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

https://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"]]
分析:

http://blog.csdn.net/linhuanmars/article/details/22777711

这里getDcit方法与上一篇中的dp数组的求解方法完全一样啊!!!

public class Solution {
public List<List<String>> partition(String s) {List<List<String>> res = new ArrayList<List<String>>();if(s==null || s.length()==0)return res;helper(s, getDict(s),0,new ArrayList<String>(), res);return res;
}
private void helper(String s, boolean[][] dict, int start, ArrayList<String> item, List<List<String>> res)
{if(start==s.length()){res.add(new ArrayList<String>(item));return;}for(int i=start;i<s.length();i++){if(dict[start][i]){item.add(s.substring(start,i+1));helper(s,dict,i+1,item,res);item.remove(item.size()-1);}}
}
private boolean[][] getDict(String s)
{boolean[][] dict = new boolean[s.length()][s.length()];for(int i=s.length()-1;i>=0;i--){for(int j=i;j<s.length();j++){if(s.charAt(i)==s.charAt(j) && ((j-i<2)||dict[i+1][j-1])){dict[i][j] = true;}}}return dict;
}
}


这篇关于**Palindrome Partitioning的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1072689

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

PostgreSQL分区表(partitioning)应用实例详解

https://www.jb51.net/article/97937.htm   PostgreSQL分区表(partitioning)应用实例详解  更新时间:2016年11月22日 10:25:58   作者:小灯光环    我要评论   这篇文章主要为大家详细介绍了PostgreSQL分区表(partitioning)应用实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

【UVA】11584-Partitioning by Palindromes(动态规划)

动态规划。 如果 j + 1 ~ i是回文,那么 dp[i] = min=(dp[j] + 1);  判断j + 1~ i是不是回文可以进行预处理,方法是枚举中心,之后向两边伸张,(需要枚举2次,一次是偶数回文,一次是奇数回文) 13993253 11584 Partitioning by Palindromes Accepted C++ 0.132 2014-08-05 08:2

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的。 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[

leetCode#125. Valid Palindrome

Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 234. Palindrome Linked List

题:https://leetcode.com/problems/palindrome-linked-list/description/ 题目 Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2Output: false Example 2: Input: 1->2->

Count Palindrome in String

string 有多少palindrome substring。exp: 'aba' 返回4 , 'abba' 返回6 public class CountPalindrome {public int countPalindrome(String str){if(str == null || str.length() == 0) return 0;int count = 0;for(int

leetcode 刷题之路 28 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. 判断一个数字是否是回文数字。 测试通过程序: class Solution {public:bool isPalindrome(int x){int revX=0;int xTemp=x;while (xTemp > 0){int tem

【LeetCode】Palindrome Partitioning III

leetcode DP 深搜 回文串 1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有可能的情况。        该问题的难度比较大,很可能第一次遇到没有思路,这很正常。下面我们一点点分析,逐步理清思路。先不考虑所有的情况,针对一个