Given A collection of numbers, return all possible permutations.
For example,
[1,2,3]The following permutations:
[1,2,3],,,,, [1,3,2] [2,1,3] [2,3,1] [3,1,2] and [3,2,1] .
The problem of permutations and combinations, regardless of whether duplicates occur, the code is as follows:
1 classSolution {2 Public:3vector<vector<int>> Permute (vector<int>&nums) {4memset (Mark,0,sizeof(Mark));5memset (CDDs,0,sizeof(CDDs));6Dfs0, Nums.size (), nums);7 returnret;8 }9 Ten voidDfsintDepintMAXDEP, vector<int> &nums) One { A if(DEP = =MAXDEP) { - tmp.clear (); - for(inti =0; i < MAXDEP; ++i) the Tmp.push_back (Cdds[i]); - Ret.push_back (TMP); -}Else{ - for(inti =0; i < MAXDEP; ++i) { + if(!Mark[i]) { -Mark[i] =true; +CDDS[DEP] =Nums[i]; ADFS (DEP +1, MAXDEP, nums); atMark[i] =false; - } - } - } - } in Private: - intcdds[ -]; to BOOLmark[ -]; +vector<int>tmp; -vector<vector<int>>ret; the *};
Leetcode Oj:permutations (Arrange)