forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## Permutations | ||
|
||
Given a collection of numbers, return all possible permutations. | ||
|
||
For example, | ||
[1,2,3] have the following permutations: | ||
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. | ||
|
||
## Solution | ||
|
||
全排列, [从一个排列求下一个排列算法](https://github.com/krystism/algorithms/tree/master/permutation) | ||
|
||
## Code | ||
```cpp | ||
class Solution { | ||
public: | ||
vector<vector<int> > permute(vector<int> &num) { | ||
vector<vector<int>> ans; | ||
sort(num.begin(), num.end()); | ||
do { | ||
vector<int> v(num.begin(), num.end()); | ||
ans.push_back(v); | ||
} while(next(num)); | ||
return ans; | ||
} | ||
private: | ||
bool next(vector<int> &v) { | ||
size_t n = v.size(); | ||
int i; | ||
for (i = n - 2; i >= 0 && v[i] >= v[i + 1]; --i); | ||
if (i < 0) { | ||
reverse(v.begin(), v.end()); | ||
return false; | ||
} | ||
int j; | ||
for (j = n - 1; j > i && v[j] <= v[i]; --j); | ||
swap(v[i], v[j]); | ||
reverse(v.begin() + i + 1, v.end()); | ||
return true; | ||
} | ||
|
||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <vector> | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
using namespace std; | ||
void print(vector<int> v) | ||
{ | ||
for (auto i : v) | ||
printf("%d ", i); | ||
printf("\n"); | ||
} | ||
class Solution { | ||
public: | ||
vector<vector<int> > permute(vector<int> &num) { | ||
vector<vector<int>> ans; | ||
sort(num.begin(), num.end()); | ||
do { | ||
vector<int> v(num.begin(), num.end()); | ||
ans.push_back(v); | ||
} while(next(num)); | ||
return ans; | ||
} | ||
private: | ||
bool next(vector<int> &v) { | ||
size_t n = v.size(); | ||
int i; | ||
for (i = n - 2; i >= 0 && v[i] >= v[i + 1]; --i); | ||
if (i < 0) { | ||
reverse(v.begin(), v.end()); | ||
return false; | ||
} | ||
int j; | ||
for (j = n - 1; j > i && v[j] <= v[i]; --j); | ||
swap(v[i], v[j]); | ||
reverse(v.begin() + i + 1, v.end()); | ||
return true; | ||
} | ||
|
||
}; | ||
int main(int argc, char **argv) | ||
{ | ||
Solution solution; | ||
vector<int> num = {1,2,3, 4, 5}; | ||
vector<vector<int>> ans = solution.permute(num); | ||
for (auto i : ans) | ||
print(i); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## Permutations II | ||
|
||
Given a collection of numbers that might contain duplicates, return all possible unique permutations. | ||
|
||
For example, | ||
`[1,1,2]` have the following unique permutations: | ||
`[1,1,2]`, `[1,2,1]`, and `[2,1,1]`. | ||
|
||
## Solution | ||
|
||
全排列, [从一个排列求下一个排列算法](https://github.com/krystism/algorithms/tree/master/permutation) | ||
|
||
## Code | ||
```cpp | ||
class Solution { | ||
public: | ||
vector<vector<int> > permuteUnique(vector<int> &num) { | ||
vector<vector<int>> ans; | ||
sort(num.begin(), num.end()); | ||
do { | ||
vector<int> v(num.begin(), num.end()); | ||
ans.push_back(v); | ||
} while(next(num)); | ||
return ans; | ||
} | ||
private: | ||
bool next(vector<int> &v) { | ||
size_t n = v.size(); | ||
int i; | ||
for (i = n - 2; i >= 0 && v[i] >= v[i + 1]; --i); | ||
if (i < 0) { | ||
reverse(v.begin(), v.end()); | ||
return false; | ||
} | ||
int j; | ||
for (j = n - 1; j > i && v[j] <= v[i]; --j); | ||
swap(v[i], v[j]); | ||
reverse(v.begin() + i + 1, v.end()); | ||
return true; | ||
} | ||
|
||
}; | ||
``` |