diff --git a/README.md b/README.md index 1eab29d..32aea76 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ + [7 Reverse Integer(溢出检测)](algorithms/ReverseInteger) + [8 String to Integer (atoi)(溢出检测)](algorithms/StringtoInteger) + [21 Merge Two Sorted Lists](algorithms/MergeTwoSortedLists) -+ [31 Next Permutation](algorithms/NextPermutation) ++ [31 Next Permutation(全排列)](algorithms/NextPermutation) ++ [46 Permutations(全排列)](algorithms/Permutations) ++ [47 Permutations II(全排列)](algorithms/Permutations2) + [60 Permutation Sequence(康托展开)](algorithms/PermutationSequence) + [65 Valid Number(细节题,状态机)](algorithms/ValidNumber) + [191 Number of 1 Bits(位运算)](algorithms/Numberof1Bits) diff --git a/algorithms/Permutations/README.md b/algorithms/Permutations/README.md new file mode 100644 index 0000000..48992ff --- /dev/null +++ b/algorithms/Permutations/README.md @@ -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 > permute(vector &num) { + vector> ans; + sort(num.begin(), num.end()); + do { + vector v(num.begin(), num.end()); + ans.push_back(v); + } while(next(num)); + return ans; + } + private: + bool next(vector &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; + } + +}; +``` diff --git a/algorithms/Permutations/permutations.cpp b/algorithms/Permutations/permutations.cpp new file mode 100644 index 0000000..0d4fbda --- /dev/null +++ b/algorithms/Permutations/permutations.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +using namespace std; +void print(vector v) +{ + for (auto i : v) + printf("%d ", i); + printf("\n"); +} +class Solution { + public: + vector > permute(vector &num) { + vector> ans; + sort(num.begin(), num.end()); + do { + vector v(num.begin(), num.end()); + ans.push_back(v); + } while(next(num)); + return ans; + } + private: + bool next(vector &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 num = {1,2,3, 4, 5}; + vector> ans = solution.permute(num); + for (auto i : ans) + print(i); + return 0; +} diff --git a/algorithms/Permutations2/README.md b/algorithms/Permutations2/README.md new file mode 100644 index 0000000..493a3da --- /dev/null +++ b/algorithms/Permutations2/README.md @@ -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 > permuteUnique(vector &num) { + vector> ans; + sort(num.begin(), num.end()); + do { + vector v(num.begin(), num.end()); + ans.push_back(v); + } while(next(num)); + return ans; + } + private: + bool next(vector &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; + } + +}; +```