Advertisement
Guest User

his

a guest
Oct 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. class Solution {
  2. public:
  3. vector<vector<int> > permute(vector<int> &num) {
  4. vector<vector<int> > result;
  5.  
  6. permuteRecursive(num, 0, result);
  7. return result;
  8. }
  9.  
  10. // permute num[begin..end]
  11. // invariant: num[0..begin-1] have been fixed/permuted
  12. void permuteRecursive(vector<int> &num, int begin, vector<vector<int> > &result) {
  13. if (begin >= num.size()) {
  14. // one permutation instance
  15. result.push_back(num);
  16. return;
  17. }
  18.  
  19. for (int i = begin; i < num.size(); i++) {
  20. swap(num[begin], num[i]);
  21. permuteRecursive(num, begin + 1, result);
  22. // reset
  23. swap(num[begin], num[i]);
  24. }
  25. }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement