-
Notifications
You must be signed in to change notification settings - Fork 0
/
46-Permutations.cpp
63 lines (60 loc) · 1.77 KB
/
46-Permutations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*=============================================================================
# FileName: 46-Permutations.cpp
# Desc: AC, 12ms
# Author: Jian Huang
# Email: [email protected]
# HomePage: https://cn.linkedin.com/in/huangjian1993
# Version: 0.0.1
# LastChange: 2015-08-08 09:22:17
# History:
=============================================================================*/
#include <leetcode.h>
class Solution {
public:
bool nextPermutation(vector<int>& nums) {
int len = nums.size(), i = len - 1, j = len - 1;
while (i > 0 && nums[i] <= nums[i - 1]) {
i --;
}
if (i == 0) {
return false;
}
i --;
while (nums[j] <= nums[i]) {
j --;
}
swap(nums[i], nums[j]);
j = len - 1, i ++;
while (i < j) {
swap(nums[i ++], nums[j --]);
}
return true;
}
vector<vector<int> > permute(vector<int>& nums) {
vector<vector<int> > result;
if (nums.empty()) {
return result;
}
sort(nums.begin(), nums.end());
result.push_back(nums);
while (nextPermutation(nums)) {
result.push_back(nums);
}
return result;
}
};
int main() {
vector<int> nums;
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);
Solution solution;
vector<vector<int> > result = solution.permute(nums);
for (int i = 0; i < (int)result.size(); i ++) {
for (int j = 0; j < (int)result[0].size(); j ++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}