Alas, it took so long, because the reverse function is not thorough enough, he changed the operand, I did not want to notice the change. Hey ╮(╯▽╰)╭, the eyes are all spent. Man, take care, self-reliance, O (∩_∩) o haha ~
The code is as follows:
classSolution { Public:voidDfs vector<int>&nums,intIndexintDepthintK vector<int>&tmpvectorint, vector<vector<int>>&result) {if(depth = = k) {return; }if(Nums.size ()-Index < k-depth)return; for(inti = index; I < nums.size (); i++) {tmpvectorint[depth] = nums[i];if(depth = = k-1) {//Missing This step allows me to spend a lot of time on this problem. Hey ╮(╯▽╰)╭ 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>>CombineintNintK) { vector<int>Nums for(inti = 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 ());returnResult }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode (combinations)