【算法与数据结构】131、LeetCode分割回文串

文章目录

所有的LeetCode题解索引,可以看这篇文章------【算法和数据结构】LeetCode题解

一、题目

二、解法

思路分析:本题仍然使用回溯算法的一般结构。加入了一个判断是否是回文串的函数,利用起始和终止索引进行判断,字符串使用引用输入, 减少传参的时间开销。将开始索引大于等于字符串长度作为终止条件,表示已经找到一个回文串的组合。此外,进一步改进算法的性能,可以建立一个查找数组,提前算出分割的子串是否为回文串,使用时直接判断即可。

cpp 复制代码
void backtracking(参数) {
    if (终止条件) {
        存放结果;
        return;
    }

    for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {
        处理节点;
        backtracking(路径,选择列表); // 递归
        回溯,撤销处理结果
    }
}

程序如下:

cpp 复制代码
class Solution {
private:
	vector<vector<string>> result;
	vector<string> path;
	bool isSymmetry(const string& s, const int start, const int end) {
		bool flag = true;
		for (int i = start, j = end; i <= j; i++, j--) {
			if (s[i] != s[j]) {
				flag = false;
				break;
			}
		}
		return flag;
	}
	void backtracking(const string& s, int startIndex) {
		if (startIndex >= s.size()) {
			result.push_back(path);
			return;
		}
		for (int i = startIndex; i < s.size(); i++) {
			if (isSymmetry(s, startIndex, i)) {	// 是回文串才加入结果数组
				string str = s.substr(startIndex, i - startIndex + 1);
				path.push_back(str);
			}
			else {	// 不是回文串跳过
				continue;
			}
			backtracking(s, i + 1);
			path.pop_back();
		}
	}
public:
	vector<vector<string>> partition(string s) {
		backtracking(s, 0);
		return result;
	}
};

复杂度分析:

  • 时间复杂度: O ( n ∗ 2 n ) O(n*2^n) O(n∗2n), n代表字符串长度。
  • 空间复杂度: O ( n 2 ) O(n^2) O(n2)。

三、完整代码

cpp 复制代码
# include <iostream>
# include <string>
# include <vector>
using namespace std;

class Solution {
private:
	vector<vector<string>> result;
	vector<string> path;
	bool isSymmetry(const string& s, const int start, const int end) {
		bool flag = true;
		for (int i = start, j = end; i <= j; i++, j--) {
			if (s[i] != s[j]) {
				flag = false;
				break;
			}
		}
		return flag;
	}
	void backtracking(const string& s, int startIndex) {
		if (startIndex >= s.size()) {
			result.push_back(path);
			return;
		}
		for (int i = startIndex; i < s.size(); i++) { // 剪枝优化
			if (isSymmetry(s, startIndex, i)) {	// 是回文串才加入结果数组
				string str = s.substr(startIndex, i - startIndex + 1);
				path.push_back(str);
			}
			else {	// 不是回文串跳过
				continue;
			}
			backtracking(s, i + 1);
			path.pop_back();
		}
	}
public:
	vector<vector<string>> partition(string s) {
		backtracking(s, 0);
		return result;
	}
};

int main() {
	string s = "aab";
	Solution s1;
	vector<vector<string>> result = s1.partition(s);
	for (vector<vector<string>>::iterator it = result.begin(); it != result.end(); it++) {
		for (vector<string>::iterator jt = (*it).begin(); jt != (*it).end(); jt++) {
			cout << *jt << " ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

end

相关推荐
我星期八休息19 小时前
深入理解跳表(Skip List):原理、实现与应用
开发语言·数据结构·人工智能·python·算法·list
lingran__19 小时前
速通ACM省铜第四天 赋源码(G-C-D, Unlucky!)
c++·算法
haogexiaole19 小时前
贪心算法python
算法·贪心算法
希望201719 小时前
图论基础知识
算法·图论
m0_7135418419 小时前
systemverilog如何解决不能使用变量索引来进行位选择的范围指定
算法·systemverilog
七牛云行业应用20 小时前
深度解析强化学习(RL):原理、算法与金融应用
人工智能·算法·金融
和编程干到底20 小时前
数据结构 栈和队列、树
数据结构·算法
纪元A梦20 小时前
贪心算法在GNN邻域采样问题中的深度解析
算法·贪心算法
宇钶宇夕20 小时前
西门子 S7-200 SMART PLC 核心指令详解:从移位、上升沿和比较指令到流水灯控制程序实战
运维·算法·自动化
爱编程的化学家20 小时前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录