`
cozilla
  • 浏览: 94034 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

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

dp[i][j]=1表示str(i~j)可以是回文。

if dp[i+1][j-1] == 1 && s[i]==s[j]; dp[i][j] = 1;

N^2扫一遍。

然后根据dp[i][j] = 1等价于 (i,j)是一条边。(i,j) 与 (j+1, x)相连。

则就变成了有向图。

然后遍历图,从0->len-1。DFS一遍。

 

class Solution {
public:
    vector<vector<int> > v;
    int n;
    string org;
    vector<vector<string>> partition(string s) {
        int len = s.size();
        n = len;
        org = s;
        int dp[len][len] ;
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < len; i++) {
            dp[i][i] = 1;
            if (i < len - 1 && s[i] == s[i+1]) dp[i][i+1] = 1;
        }
        
        for (int l = 2; l < len; l++) {
            for (int i = 0; i < len; i++) {
                int j = i+l;
                if (j >= len) continue;
                if (dp[i+1][j-1] == 1 && s[i] == s[j]) dp[i][j] = 1;
            }
        }
        v.clear();
        v.resize(len);
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
                if (dp[i][j] == 1) v[i].push_back(j);
            }
        }
        
        vector<vector<string> > res;
        vector<string> str;
        f(res, str, -1);
        return res;
    }
    
    void f(vector<vector<string> >& res, vector<string>& str, int cur) {
        if (cur == n - 1) {
            res.push_back(str);
            return;
        }
        
        int next = cur+1;
        for (int i = 0; i < v[next].size(); i++) {
            str.push_back(org.substr(next, v[next][i]-next+1));
            f(res, str, v[next][i]);
            str.pop_back();
        }
    }
};

 

分享到:
评论

相关推荐

    python-leetcode题解之132-Palindrome-Partitioning-II

    在众多的LeetCode题库中,“Palindrome Partitioning II” 是一个有趣的字符串处理问题,属于动态规划和字符串分割的范畴。这个问题要求我们编写一个函数,接受一个字符串s作为输入,然后找到使字符串s的子串都是...

    js-leetcode题解之131-palindrome-partitioning.js

    在JavaScript中,处理leetcode上的编程题目时,正确理解和解决"131.Palindrome Partitioning"这类字符串划分问题是一项重要的技能。本题要求编写一个函数,该函数将给定的字符串划分为尽可能多的回文子字符串,并...

    python-leetcode题解之131-Palindrome-Partitioning

    4. Palindrome Partitioning(回文划分):是本题的核心问题,即要求将给定的字符串划分为尽可能多的回文子串。回文是指正读和反读都相同的字符串。 5. 动态规划(Dynamic Programming):一种算法设计方法,它将...

    js-leetcode题解之132-palindrome-partitioning-ii.js

    JavaScript解决方案专题——LeetCode题解之132题——分割回文串 II(palindrome-partitioning-ii.js) 在本篇文章中,我们将会探讨LeetCode中的132题——分割回文串 II。此题目要求我们对给定的字符串进行分割,...

    Leetcode部分解答(126题)

    1. **Palindrome Partitioning (mine).cpp** - 这个文件是关于LeetCode的第131题“回文分割”。该问题要求将一个字符串分割成多个回文子串。解冑通常涉及深度优先搜索或动态规划,寻找所有可能的分割方案并检查是否...

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 ...Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar

    數位之和leetcode-leetcode-cpp:我的LeetCodeC++答案

    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:leetcodebytags按自己整理的一些类别

    leetcode力扣是什么 leetcode-按类别 看了一些leetcode刷题指南,总结一下两个重点,一是最好按照类别刷题,总结思路;...Partitioning (hard) 212 Word Search II (hard) DFS /二叉树 the difference between df

    leetcode添加元素使和等于-leetcode:leetcode

    Palindrome_Partitioning DP,避免递归调用,利用数组储存已算出来的数值,由后向前逐渐增加字符串处理 Palindrome_Partitioning_2 同上 Sum_Root_to_Leaf_Numbers DFS Surrounded_Regions 由四周‘O’开始向内检索...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    gas station leetcode 在牛客网上的在线编程...palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca

    leetcode338-LeetCode:力码

    分割回文串(Palindrome Partitioning)**:这是一个关于字符串处理的题目,要求将一个字符串分割成若干个回文子串。主要涉及的算法是深度优先搜索(DFS),通过递归的方式检查所有可能的分割方案,判断是否都是回文。 ...

    Selective_Leetcode_solutions

    3. 题目131 - "Palindrome Partitioning" 这是一道回文子串分割问题,要求将一个字符串分割成多个回文子串。常见的解法是动态规划,创建一个二维数组来存储子问题的最优解。在Java中,可以使用StringBuilder和...

    PythonAlgo:用Python解决Leetcode问题

    对于更高级的题目,可能会涉及图论或动态规划,比如`0131_palindrome_partitioning.py`可能涉及回文子串分割,这通常会用到动态规划来避免重复计算。 在PythonAlgo项目中,每个问题的解决方案都提供了清晰的思路和...

Global site tag (gtag.js) - Google Analytics