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

leetcode 131. Palindrome Partitioning

目录

一、题目描述

二、方法1、回溯法+每次暴力判断回文子串

三、方法2、动态规划+回溯法


一、题目描述

分割回文子串

131. Palindrome Partitioning

二、方法1、回溯法+每次暴力判断回文子串

class Solution {vector<vector<string>> res;vector<string> path;
public:vector<vector<string>> partition(string s) {backtracking(s,0);return res;}void backtracking(string &s,int start){if(start == s.size()){res.push_back(path);return;}for(int i = start;i < s.size();i++){string cur = s.substr(start,i-start+1);if(!isPalindrome(cur))continue;path.push_back(cur);backtracking(s,i+1);path.pop_back();}}bool isPalindrome(const string &str){int left = 0;int right = str.size()-1;while(left<=right){if(str[left]!= str[right])return false;left++;right--;}return true;}
};

三、方法2、动态规划+回溯法

先用动态规划法,求出s[i,j]是否是回文子串,后面直接查表判断回文子串。

参考leetcode 647. Palindromic Substrings-CSDN博客

class Solution {vector<vector<string>> res;vector<string> path;vector<vector<bool>> dp;
public:vector<vector<string>> partition(string s) {int n = s.size();//0 <= i<=j <= n-1//dp[i][j]表示s[i,j]是否是回文子串,其中i<=j,i>j的dp[i][j]不定义dp.resize(n,vector<bool>(n,false));for(int i = n-1;i>=0;i--){for(int j = i;j<n;j++){if(s[i] == s[j]){if(j-i <= 1){dp[i][j] = true;}else if(dp[i+1][j-1] == true){dp[i][j] = true;}}}}backtracking(s,0);return res;}void backtracking(string &s,int start){if(start == s.size()){res.push_back(path);return;}for(int i = start;i < s.size();i++){string cur = s.substr(start,i-start+1);// if(!isPalindrome(cur))//     continue;if(!dp[start][i])continue;path.push_back(cur);backtracking(s,i+1);path.pop_back();}}// bool isPalindrome(const string &str){//     int left = 0;//     int right = str.size()-1;//     while(left<=right){//         if(str[left]!= str[right])//             return false;//         left++;//         right--;//     }//     return true;// }
};

文章转载自:
http://approbatory.hyyxsc.cn
http://calibre.hyyxsc.cn
http://ceremonialism.hyyxsc.cn
http://childing.hyyxsc.cn
http://biometry.hyyxsc.cn
http://acetifier.hyyxsc.cn
http://apoplexy.hyyxsc.cn
http://cerebrospinal.hyyxsc.cn
http://carpeting.hyyxsc.cn
http://alkalemia.hyyxsc.cn
http://affectless.hyyxsc.cn
http://biquadratic.hyyxsc.cn
http://catafalque.hyyxsc.cn
http://appendix.hyyxsc.cn
http://cardiotoxic.hyyxsc.cn
http://biophysics.hyyxsc.cn
http://attendance.hyyxsc.cn
http://abaya.hyyxsc.cn
http://campanulate.hyyxsc.cn
http://besom.hyyxsc.cn
http://charlene.hyyxsc.cn
http://almemar.hyyxsc.cn
http://altherbosa.hyyxsc.cn
http://apiculturist.hyyxsc.cn
http://absentmindedly.hyyxsc.cn
http://airt.hyyxsc.cn
http://chemiculture.hyyxsc.cn
http://awfulness.hyyxsc.cn
http://aeroengine.hyyxsc.cn
http://caddice.hyyxsc.cn
http://www.dtcms.com/a/212826.html

相关文章:

  • 【Qt】QCustomPlot相关
  • 2025一带一路暨金砖国家技能发展与技术创新大赛第三届企业信息系统安全赛项
  • 【面板数据】上市公司外资持股数据集(2005-2023年)
  • 防火墙高可用(HA)主备验证实验(eNSP)
  • TTL和死信交换机实现延迟队列
  • 4款顶级磁力下载工具,速度提升器,可以变下变播
  • 第三章 第二大脑的运作机理 整理笔记
  • 套索回归与岭回归通俗讲解
  • TCP建立连接为什么不是两次握手,而是三次,为什么不能在第二次握手时就建立连接?
  • uniapp-商城-68-shop(1-商品列表,获取数据,utils、tofixed 、parseInt的使用)
  • Python容器
  • 基于 LangChain + Chroma 实现文档向量化入库(含摘要处理 + RAG 查询):完整实战流程
  • Linux基本指令篇 —— cd指令
  • 【TypeScript】结构化类型系统与标明类型系统
  • [Protobuf] 快速上手:安全高效的序列化指南
  • Anaconda 常用命令汇总
  • RocketMQ核心特性与最佳实践
  • 用HTML5实现实时ASCII艺术摄像头
  • QT6安装与概念介绍
  • 汉诺塔超级计算机堆栈区与数据区的协同
  • 【Linux】系统程序−进度条
  • MySQ里的主从复制
  • 深入探索AI模型工程:算法三大门派与监督学习的魅力
  • python的pip怎么配置的国内镜像
  • 深入理解设计模式之装饰器模式
  • 网络段、主机段、子网掩码
  • 【网络安全】轻量敏感路径扫描工具
  • MySQL权限管理:层级化作用域、权限分类、操作命令
  • 5GC网络中的QoS Flow级QoS控制
  • Baklib领跑五款知识管理工具评测