当前位置: 首页 > news >正文

Leetcode-131.Palindrome Partitioning [C++][Java]

目录

一、题目描述

二、解题思路

【C++】

【Java】


Leetcode-131.Palindrome Partitioninghttps://leetcode.com/problems/palindrome-partitioning/description/131. 分割回文串 - 力扣(LeetCode)131. 分割回文串 - 给你一个字符串 s,请你将 s 分割成一些 子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。 示例 1:输入:s = "aab"输出:[["a","a","b"],["aa","b"]]示例 2:输入:s = "a"输出:[["a"]] 提示: * 1 <= s.length <= 16 * s 仅由小写英文字母组成https://leetcode.cn/problems/palindrome-partitioning/description/

一、题目描述

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

Example 1:

Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]

Example 2:

Input: s = "a"
Output: [["a"]]

Constraints:

  • 1 <= s.length <= 16
  • s contains only lowercase English letters.

二、解题思路

  • 时间复杂度:O(n⋅2^n)

  • 空间复杂度:O(n^2)

【C++】

class Solution {
private:
    void dfs(const string& s, int start, vector<string>& pat, vector<vector<string>>& res) {
        if (start == s.length()) {res.push_back(pat);}
        else {
            for (int i = start; i < s.length(); i++) {
                if (isPalindrome(s, start, i)) {
                    pat.push_back(s.substr(start, i - start + 1));
                    dfs(s, i + 1, pat, res);
                    pat.pop_back();
                }
            }
        }
    }

    bool isPalindrome(const string& s, int l, int r) {
        while (l <= r) if (s[l++] != s[r--]) return false;
        return true;
    }

public:
    vector<vector<string>> partition(string s) {
        vector<vector<string>> res;
        vector<string> pat;      
        dfs(s, 0, pat, res);
        return res;
    }
};

【Java】

class Solution {
    private void dfs(String s, int start, List<String> pat, List<List<String>> res) {
        if (start == s.length()) {res.add(new ArrayList<>(pat));}
        else {
            for (int i = start; i < s.length(); i++) {
                if (isPalindrome(s, start, i)) {
                    pat.add(s.substring(start, i + 1));
                    dfs(s, i + 1, pat, res);
                    pat.remove(pat.size() - 1);
                }
            }
        }
    }

    private boolean isPalindrome(String s, int l, int r) {
        while (l <= r) if (s.charAt(l++) != s.charAt(r--)) return false;
        return true;
    }

    public List<List<String>> partition(String s) {
        List<List<String>> res = new ArrayList<>();
        List<String> pat = new ArrayList<>();
        dfs(s, 0, pat, res);
        return res;
    }
}

相关文章:

  • RUOYI框架在实际项目中的应用三:Ruoyi微服务版本-RuoYi-Cloud
  • JAVA数据库技术(一)
  • Deepseek学习--工具篇之Ollama
  • 基于C#的以太网通讯实现:TcpClient异步通讯详解
  • 设置echarts legend 图例与文字对齐
  • 股指期货有卖不出去的时候吗?
  • 在线 SQL 转 flask SQLAlchemy 模型
  • ctf web入门知识合集
  • 阿里wan2.1本地部署
  • Webpack总结
  • MySQL配置文件my.cnf详解
  • 抽象工厂模式 (Abstract Factory Pattern)
  • 蓝桥杯专项复习——结构体、输入输出
  • 花生好车:重构汽车新零售生态的破局者
  • HTML5前端第三章节
  • Centos离线安装openssl-devel
  • 【深度学习与大模型基础】第5章-线性相关与生成子空间
  • 音视频缓存数学模型
  • AI-医学影像分割方法与流程
  • Spring Validation参数校验
  • 世卫大会中国代表团:中国深入参与全球卫生治理,为构建人类卫生健康共同体贡献中国力量
  • 特写|银耳种植“北移”到沧州盐山,村民入伙可年增收4万元
  • 英德宣布开发射程超2000公里导弹,以防务合作加强安全、促进经济
  • 南昌上饶领导干部任前公示:2人拟提名为县(市、区)长候选人
  • 外企聊营商|武田制药:知识产权保护助创新药研发
  • 中国青年报:为见义勇为者安排补考,体现了教育的本质目标