当前位置:网站首页>力扣练习——38 分割回文串
力扣练习——38 分割回文串
2022-08-02 04:18:00 【qq_43403657】
38 分割回文串
1.问题描述
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案的数量。
示例:
输入: “aab”
输出: 2
说明:可能的分割方案有:
[
[“aa”,“b”],
[“a”,“a”,“b”]
]
2.输入说明
输入一个字符串 s,长度小于等于200.
3.输出说明
输出一个整数
4.范例
输入
aab
输出
2
5.代码
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
vector<vector<string>>res;//结果数组
vector<string>temp;
bool isPalin(string s)
{
int n = s.length();
for (int i = 0; i < n/2; i++) //注意,这里只要遍历到一半即可
{
if (s[i] != s[n - 1 - i])
return false;
}
return true;
}
void backtrace(string s, int index)
{
int len = s.length();
if (index == len)//遍历到结尾
{
res.push_back(temp);
return;
}
for (int i = index; i < len; i++)
{
if (isPalin(s.substr(index, i - index + 1)))//重点理解下这里提取子串的操作
{
//访问到下标index,然后往后提取长度为1,2,etc的子串
//当i==index时,提取子串长度为1
temp.push_back(s.substr(index, i - index + 1));
backtrace(s, i + 1);//遍历到下一个位置
temp.pop_back();//回溯
}
}
}
int num_Partition(string s)
{
int len = s.length();
if (len == 0)
return 1;
//进行遍历回溯操作
backtrace(s, 0);
return res.size();
}
int main()
{
string s;
cin >> s;
int res = num_Partition(s);
cout << res << endl;
return 0;
}
边栏推荐
- MapFi paper structure organization
- JDBC再回顾
- 【FreeRTOS】12 任务通知——更省资源的同步方式
- 不会多线程还想进 BAT?精选 19 道多线程面试题,有答案边看边学
- Visual SLAM Lecture Fourteen - Lecture 13 Practice: Designing a SLAM system (the most detailed code debugging and running steps)
- Use the advanced timer of GD32F207 to generate hidden bugs in PWM waves
- 数据复制系统设计(3)-配置新的从节点及故障切换
- C语言特殊运算符
- 8月1日“海豹数藏”将全网首发民族英雄林则徐《四行行书》数字藏品!
- Scientific research notes (5) SLAC WiFi Fingerprint+ Step counter fusion positioning
猜你喜欢
无主复制系统(2)-读写quorum
无主复制系统(1)-节点故障时写DB
ADSP21489数据手册表摘要
数据复制系统设计(2)-同步复制与异步复制
EasyCVR视频广场切换通道,视频播放协议异常的问题修复
Go 语言是如何实现切片扩容的?【slice】
ROS visualization of 3D target detection
Batch normalization (BN) based on deep learning
复制延迟案例(4)-一致前缀读
ScholarOne Manuscripts submits journal LaTeX file and cannot convert PDF successfully!
随机推荐
CaDDN paper reading of monocular 3D target detection
STM32 OLED显示屏--SPI通信知识汇总
nr部分计算
不会多线程还想进 BAT?精选 19 道多线程面试题,有答案边看边学
EasyCVR视频广场切换通道,视频播放协议异常的问题修复
2022-08-01:以下go语言代码输出什么?A:panic;B:5;C:6;D:编译错误。 package main import ( “fmt“ ) func main() {
线代005
【面试】招聘要求
Learn about the sequential storage structure of binary tree - heap
批量--09---批量读文件入表
【STM32】ADC采集光敏数据(不看库函数手册进行配置)
Qt编写物联网管理平台49-设备模拟工具
七月阅读:《刘慈欣科幻短篇小说集Ⅰ》笔记
OpenPCDet environment configuration of 3 d object detection and demo test
如何让固定点的监控设备在EasyCVR平台GIS电子地图上显示地理位置?
其他重要协议(DNS,ICMP,NAT,交换机)
Deep Blue Academy - Visual SLAM Lecture Fourteen - Chapter 5 Homework
C# Thread IsBackground作用
轮询和长轮询的区别
跑通CogView教程