Leetcode#131 Palindrome Partitioning

原题地址

因为要找所有的解,只能搜索+回溯了

看来数据量比较小,关于回文串的判断没有使用动态规划也可以过

代码:

 1 vector<vector<string> > res;
 2
 3 bool palindromep(string s) {
 4   int i = 0;
 5   int j = s.length() - 1;
 6   while (i < j && s[i] == s[j]) {
 7     i++;
 8     j--;
 9   }
10   return i >= j;
11 }
12
13 void dfs(string s, vector<string> ans, int pos) {
14   if (pos == s.length())
15     res.push_back(ans);
16   for (int len = 1; pos + len <= s.length(); len++) {
17     if (palindromep(s.substr(pos, len))) {
18       ans.push_back(s.substr(pos, len));
19       dfs(s, ans, pos + len);
20       ans.pop_back();
21     }
22   }
23 }
24
25 vector<vector<string>> partition(string s) {
26   dfs(s, vector<string>(), 0);
27   return res;
28 }
时间: 2024-10-08 22:08:35

Leetcode#131 Palindrome Partitioning的相关文章

Java for LeetCode 131 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",

leetcode之Palindrome Partitioning

方法一:DFS递归,判断每一个是否为回文数1,首先要有一个判断字符串是否是回文的函数.容易实现,字符串从两边同时往中间走,看字符是否相同; 2,深度优先搜索思想对字符串进行遍历.得到结果.例如,s = "abacd"; 需要对"a""ad""aba""abac""abacd"进行深度优先搜索.深度搜索的过程如下:先选"a",发现"a"是回文,则深度

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

[LeetCode][Java] 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",&

【算法】leetcode之 Palindrome Partitioning I&amp;II(转载)

1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有可能的情况. 该问题的难度比较大,很可能第一次遇到没有思路,这很正常.下面我们一点点分析,逐步理清思路.先不考虑所有的情况,针对一个符合条件的划分,每一部分都是一个回文子串,而且各部分的长度不固定.也即每一部分都是原始字符串的一个子串,且满足回文条件.所有的划分都满足上述条件,所以这就启发我们首先判

【leetcode】Palindrome Partitioning

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&qu

【LeetCode】Palindrome Partitioning II

Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome p

leetcode 131. Palindrome Partitioning----- java

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","

Java for LeetCode 132 Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"