void backtrace(vector<int>&record, vector<vector<int>>&res, int index, vector<int>path)
{
if (path.size() == 2)
{
res.push_back(path);
return;
}
for (int i = index; i < record.size(); i++)
{
path.push_back(record[i]);
backtrace(record, res, i + 1, path);
path.pop_back();
}
}

int main()
{
vector<int>record = { 5,4,3,2,1 };
vector<vector<int>>res;
vector<int>path;
backtrace(record,res,0,path);
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++)
{
for (int j = 0; j < res[0].size(); j++)
{
cout << res[i][j] << " ";
}
cout << endl;
}
}

 输出结果如下:

一个基本的回溯及其演变_i++


​77. 组合​

​面试题---求和​

​leetcode刷题之回溯,深度遍历​