分割字符串爲迴文串(每一個子串都是迴文串)

void dfs(string s, vector<string> &path, vector<vector<string>> &res)
	{
		if(s.size() < 1)
		{
			res.push_back(path);
			return;
		}
		for(int i = 0; i < s.size(); i++)
		{
			int begin = 0;
			int end = i;
			while(begin < end)
			{
				if(s[begin] == s[end])
				{
					begin++;
					end--;
				}
				else
					break;
			}
			if(begin >= end)//bool isPalindrome = true;
			{
				path.push_back(s.substr(0,i+1));
				dfs(s.substr(i+1),path,res);
				path.pop_back();
			}
		}
	}
	vector<vector<string>> partition(string s) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        vector<vector<string>> res;
		vector<string> path;
		dfs(s,path,res);
		return res;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章