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

Leetcode-132.Palindrome Partitioning II [C++][Java]

目录

一、题目描述

二、解题思路

【C++】

【Java】


Leetcode-132.Palindrome Partitioning IIhttps://leetcode.com/problems/palindrome-partitioning-ii/description/132. 分割回文串 II - 力扣(LeetCode)132. 分割回文串 II - 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文串。返回符合要求的 最少分割次数 。 示例 1:输入:s = "aab"输出:1解释:只需一次分割就可将 s 分割成 ["aa","b"] 这样两个回文子串。示例 2:输入:s = "a"输出:0示例 3:输入:s = "ab"输出:1 提示: * 1 <= s.length <= 2000 * s 仅由小写英文字母组成https://leetcode.cn/problems/palindrome-partitioning-ii/description/

一、题目描述

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.

Example 1:

Input: s = "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.

Example 2:

Input: s = "a"
Output: 0

Example 3:

Input: s = "ab"
Output: 1

Constraints:

  • 1 <= s.length <= 2000
  • s consists of lowercase English letters only.

二、解题思路

方法一

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

  • 空间复杂度:O(n)

【C++】

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

public:
    int minCut(string s) {
        vector<int> f(s.size(), INT_MAX);
        for (int i = 0; i < s.size(); ++i) {
            if (isPalindrome(s, 0, i)) {f[i] = 0;}
            else {
                for (int j = 0; j < i; ++j) {
                    if (isPalindrome(s, j + 1, i)) {
                        f[i] = min(f[i], f[j] + 1);
                    }
                }
            }
        }
        return f[s.size() - 1];
    }
};

【Java】

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

    public int minCut(String s) {
        int[] f = new int[s.length()];
        Arrays.fill(f, Integer.MAX_VALUE);
        for (int i = 0; i < s.length(); ++i) {
            if (isPalindrome(s, 0, i)) {f[i] = 0;}
            else {
                for (int j = 0; j < i; ++j) {
                    if (isPalindrome(s, j + 1, i)) {
                        f[i] = Math.min(f[i], f[j] + 1);
                    }
                }
            }
        }
        return f[s.length() - 1];
    }
}

方法二

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

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

【C++】

class Solution {
public:
    int minCut(string s) {
        vector<vector<int>> isPalindrome(s.size(), vector<int>(s.size(), true));
        for (int i = s.size() - 1; i >= 0; --i) {
            for (int j = i + 1; j < s.size(); ++j) {
                isPalindrome[i][j] = (s[i] == s[j]) && isPalindrome[i + 1][j - 1];
            }
        }
        vector<int> f(s.size(), INT_MAX);
        for (int i = 0; i < s.size(); ++i) {
            if (isPalindrome[0][i]) {f[i] = 0;}
            else {
                for (int j = 0; j < i; ++j) {
                    if (isPalindrome[j + 1][i]) {
                        f[i] = min(f[i], f[j] + 1);
                    }
                }
            }
        }
        return f[s.size() - 1];
    }
};

【Java】

class Solution {
    public int minCut(String s) {
        int[] f = new int[s.length()];
        boolean[][] isPalindrome = new boolean[s.length()][s.length()];
        for (int l = s.length() - 1; l >= 0; --l) {
            for (int r = l; r < s.length(); ++r) {
                isPalindrome[l][r] = (l == r)
                    ? true
                    : (s.charAt(l) == s.charAt(r)) && (l + 1 == r || isPalindrome[l + 1][r - 1]);
            }
        }
        for (int i = 0; i < s.length(); ++i) {
            f[i] = Integer.MAX_VALUE;
            if (isPalindrome[0][i]) {f[i] = 0;}
            else {
                for (int j = 0; j < i; ++j) {
                    if (isPalindrome[j + 1][i]) {
                        f[i] = Math.min(f[i], f[j] + 1);
                    }
                }
            }
        }
        return f[s.length() - 1];
    }
}

相关文章:

  • 如何在PyCharm中利用Python对象自动提示提高开发效率?
  • 数学建模 第二节
  • 删除二叉搜索树中的节点
  • 第五章-动态规划
  • 实践 PyTorch 手写数字识别
  • 机试准备第17天
  • Suno的对手Luno:AI音乐开发「上传参考音频 - 方式一:通过二进制流的方式」 —— 「Luno Api系列|AI音乐API」第11篇
  • 【NLP 38、实践 ⑩ NER 命名实体识别任务 Bert 实现】
  • Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
  • Springboot中的 Mapper 无法找到的 可能原因及解决方案
  • 一个简单的井字棋(Tic-Tac-Toe)游戏的C语言实现
  • 程序化广告行业(20/89):交易模式深度剖析与价值解读
  • 基于51单片机的多功能时钟闹钟proteus仿真
  • 前端内存优化实战指南:从内存泄漏到性能巅峰
  • IMX6ULL_Pro开发板的串口应用程序实例(利用TTY子系统去使用串口)
  • 蓝桥杯[阶段总结] 二分,前缀和
  • C语言动态内存管理(上)
  • Compose 实践与探索十二 —— 附带效应
  • Webpack 基础
  • SLC跨头协作机制
  • 龙湖集团:今年前4个月销售220.8亿元,4月新增两块土地储备
  • 印称一名高级官员在巴基斯坦发动的袭击中死亡
  • 5天完成1000多万元交易额,“一张手机膜”畅销海内外的启示
  • 19岁女生注射头孢离世后续:院方道歉,医生停职,监管介入
  • 特朗普政府拟终止太空污染研究,马斯克旗下太空公司将受益
  • 深入贯彻中央八项规定精神学习教育中央第七指导组指导督导中国船舶集团见面会召开