标签:
哎,花了这么长时间,竟然是因为reverse函数掌握不够透彻,他改变了操作数,我竟然全然没想注意到这一变化。哎╮(╯▽╰)╭,眼睛都花了。男人嘛,保重,自强,O(∩_∩)O哈哈~
代码如下:
class Solution {
public:
void dfs(vector<int> &nums, int index, int depth, int k, vector<int> &tmpVectorint, vector<vector<int>> &result) {
if(depth == k) {
return;
}
if(nums.size() - index < k - depth)
return;
for(int i = index; i < nums.size(); i++) {
tmpVectorint[depth] = nums[i];
if(depth == k - 1) {
//缺少这个步骤让我在这道题上花费好多时间。哎╮(╯▽╰)╭
vector<int> tmp(tmpVectorint.begin(), tmpVectorint.end());
reverse(tmp.begin(), tmp.end());
cout << tmp[0] << endl;
cout << tmp[1] << endl;
result.push_back(tmp);
continue;
}
dfs(nums, i + 1, depth + 1, k, tmpVectorint, result);
}
}
vector<vector<int>> combine(int n, int k) {
vector<int> nums;
for(int i = n; i > 0; i--)
nums.push_back(i);
vector<vector<int>> result;
vector<int> tmpVectorint(k, 0);
dfs(nums, 0, 0, k, tmpVectorint, result);
reverse(result.begin(), result.end());
return result;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/guanzhongshan/article/details/47126277