【算法与数据结构】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

相关推荐
微石科技11 小时前
长期慢病如何做好居家管理?宁波微石科技星梦云康改善周期数据零散短板
大数据·科技·物联网·算法
微露清风11 小时前
快速排序算法学习记录
学习·算法·排序算法
梓䈑17 小时前
【算法题攻略】BFS 解决FloodFill 算法、最短路问题、多源BFS 和 拓扑排序
c++·算法·宽度优先
Adios79419 小时前
设置交集大小至少为2
数据结构·算法·leetcode
来一碗刘肉面1 天前
栈的应用(表达式求值)
数据结构·算法
xiaowang1234shs1 天前
怪兽轻断食技术深度测评:从断食计时引擎到AI识别算法的工程实践解析
数据库·人工智能·算法·macos·机器学习·p2p·visual studio
小果因子实验室1 天前
量化研究--策略迁移算法1研究
算法
Web极客码1 天前
如何用三段式确定性剪枝,为 LLM Agent 砍掉 35% 的 Token 成本?
服务器·人工智能·算法·机器学习
程序猿乐锅1 天前
【数据结构与算法 | 第六篇】力扣1109,1094差分数组
java·算法·leetcode