红桃J

用心写好每行完美的代码,远比写一堆更有价值

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

代码:

 1 #include<iostream>
 2 #include<vector>
 3 
 4 using namespace std;
 5 
 6 void getResult(vector<vector<int>> &result, vector<int> element, vector<int> nums)
 7 {
 8     if (element.size() == nums.size())
 9     {
10         result.push_back(element);
11     }
12     else
13     {
14         for (int i = 0; i < nums.size(); i++)
15         {
16             if (find(element.begin(), element.end(), nums[i]) == element.end())
17             {
18                 element.push_back(nums[i]);
19                 getResult(result, element, nums);
20                 element.pop_back();
21             }
22         }
23     }
24 }
25 
26 vector<vector<int>> permute(vector<int>& nums) 
27 {
28     vector<vector<int>> result;
29     vector<int> element;
30     getResult(result, element, nums);
31     return result;
32 }
33 
34 int main()
35 {
36     vector<int> nums = {1,2, 3};
37     vector<vector<int>> result = permute(nums);
38     cout << result.size() << endl;
39     for (int i = 0; i < result.size(); i++)
40     {
41         for (int j = 0; j < result[i].size(); j++)
42             cout << result[i][j] << "    ";
43         cout << endl;
44     }
45 }

 

posted on 2015-06-15 18:46  红桃J  阅读(167)  评论(0编辑  收藏  举报