《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind
来源:互联网 发布:天刀男捏脸数据下载 编辑:程序博客网 时间:2024/05/16 15:11
题目:Given a strings, 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"]
]
结果:牛客网编译通过
代码及思路:
//中心思想:固定第一个子串,如果第一个子串是回文,后面的子串用递归进行判断是否是回文,
//先循环找第一个子串,可能是前1个、前两个。。。前n个,固定第一个子串了,进行判断,
//如果是回文,递归继续求第一个子串固定了的后面的子串,递归完毕将第一个子串从字符串数组中弹出,
//递归结束的条件就是,判断到最后一个子串都是回文了,把字符串数组放到要返回的结果数组中。
vector<vector<string>> partition(string s) {
vector<vector<string>> vvs;
vector<string> vs;
par(vvs,vs,s);
return vvs;
}
void par(vector<vector<string>> &vvs,vector<string> &vs,string &s){ //参数用引用
if(s.empty()){
vvs.push_back(vs); //如果递归到了字符串最后,说明子串全是回文,放入要返回的二维数组中
return;
}
for(int i=1;i<=s.length();i++){ //循环确定第一个子串
string a = s.substr(0,i);
if(is(a)){ //如果第一个子串是回文才往后面递归
vs.push_back(a); //放入字符串数组中
string b = s.substr(i);
par(vvs,vs,b);
vs.pop_back(); //从字符串数组中取出来,因为下次从头开始还用这个变量来存下一批回文子串
}
}
}
0 0
- 《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind
- 《牛客网leetcode144题》Given a string S, find the longest palindromic substring in S
- Write a method that counts the number of occurrences of a given character in a string
- [ZOJ1460]The Partition of a Cake
- zoj 1460 The Partition of a Cake
- Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa
- Given a string, find the length of the longest substring without repeating characters
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- If t is the subString of s
- Partition a set of numbers into two so that difference between their sum is minimum with equal size
- A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi
- Your boot partition is on a disk using the GPT
- Find the number of subsets such that the sum of numbers in the subset is a prime number
- ZOJ 1460 The Partition of a Cake 线段相交
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Max Number of A's given four keys
- CareerCup Given an array A[], find (i, j) such that A[i] < A[j] and (j - i) is maximum.
- mysql 添加外键 遇到的问题
- 【PAT】1109. Group Photo
- 记录完成freeCodeCamp的过程
- Java基础--对象概述
- vijos 1053 spfa 判断负环
- 《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind
- spring所有jar
- 配置Hibernate和MySQL
- Android编程常见报错和自动补全配置
- 依赖错误
- python代码实现简易滤除数字方法
- 九度 题目1179:阶乘
- CSS选择器
- 反射示例1:通过配置文件运行类中的方法