LeetCode – Refresh – Palindrome Partitioning I

 1 class Solution {
 2 public:
 3     bool isP(string s) {
 4         int start = 0, end = s.size()-1;
 5         while (start < end) {
 6             if (s[start++] != s[end--]) return false;
 7         }
 8         return true;
 9     }
10     void getP(vector<vector<string> > &result, vector<string> current, string s, int index) {
11         if (index == s.size()) {
12             result.push_back(current);
13             return;
14         }
15         for (int i = index; i < s.size(); i++) {
16             if (isP(s.substr(index, i-index+1))) {
17                 current.push_back(s.substr(index, i-index+1));
18                 getP(result, current, s, i+1);
19                 current.pop_back();
20             }
21         }
22     }
23     vector<vector<string>> partition(string s) {
24         vector<vector<string> > result;
25         getP(result, vector<string> (), s, 0);
26         return result;
27     }
28 };
时间: 2024-10-09 00:12:01

LeetCode – Refresh – Palindrome Partitioning I的相关文章

LeetCode – Refresh – Palindrome Partitioning II

Notes: 1. If len < 2, return 0. It is not minimum length but how many cuts. 2. As it was cutting inside of string. Final result is dp[0] - 1, since there is no cut at most left. 1 class Solution { 2 public: 3 int minCut(string s) { 4 int len = s.size

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

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"

leetcode dfs Palindrome Partitioning

Palindrome Partitioning Total Accepted: 21056 Total Submissions: 81036My Submissions 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 = "a