From aedd3cb874eeb56ff1714b71dfac2650b71c0b16 Mon Sep 17 00:00:00 2001 From: ruitaocai Date: Sun, 9 Aug 2015 14:43:56 +0800 Subject: [PATCH 1/2] another solution --- Permutations.h | 36 ++++++++++++++++++++++++++++++++++++ PermutationsII.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/Permutations.h b/Permutations.h index 3a34865..40856dd 100644 --- a/Permutations.h +++ b/Permutations.h @@ -12,6 +12,10 @@ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. Solution: dfs... + Solution2:non-recursive and unique result. Find the next permutation and add it to result set, + this will miss permutations that 'smaller' than the given 'nums'. + So, at first,we sort the nums. This is the fastest one in all test case. the dfs's + result may contain duplicate permutations. */ class Solution { @@ -46,3 +50,35 @@ class Solution { } } }; + +class Solution2 { +public: + //return all possible unique permutations. + vector> permute(vector& nums) { + sort(nums.begin(), nums.end());//!!! + vector> ret; + ret.push_back(nums); + while (1) + { + int idx = -1; + for (int i = nums.size() - 1; i > 0; i--){ + if (nums[i] > nums[i - 1]){ + idx = i - 1; + break; + } + } + if (idx == -1) break; + for (int i = nums.size() - 1; i > 0; i--){ + if (nums[i] > nums[idx]){ + int tmp = nums[i]; + nums[i] = nums[idx]; + nums[idx] = tmp; + break; + } + } + sort(&nums[0] + idx + 1, &nums[0] + nums.size()); + ret.push_back(nums); + } + return ret; + } +}; \ No newline at end of file diff --git a/PermutationsII.h b/PermutationsII.h index dbea526..9619344 100644 --- a/PermutationsII.h +++ b/PermutationsII.h @@ -12,6 +12,9 @@ [1,1,2], [1,2,1], and [2,1,1]. Solution: dfs... + Solution2:non-recursive. Find the next permutation and add it to result set, + this will miss permutations that 'smaller' than the given 'nums'. + So, at first,we sort the nums. This is the fastest one in all test case. */ class Solution { @@ -49,3 +52,35 @@ class Solution { } } }; + +class Solution2 { +public: + //return all possible unique permutations. + vector> permute(vector& nums) { + sort(nums.begin(), nums.end());//!!! + vector> ret; + ret.push_back(nums); + while (1) + { + int idx = -1; + for (int i = nums.size() - 1; i > 0; i--){ + if (nums[i] > nums[i - 1]){ + idx = i - 1; + break; + } + } + if (idx == -1) break; + for (int i = nums.size() - 1; i > 0; i--){ + if (nums[i] > nums[idx]){ + int tmp = nums[i]; + nums[i] = nums[idx]; + nums[idx] = tmp; + break; + } + } + sort(&nums[0] + idx + 1, &nums[0] + nums.size()); + ret.push_back(nums); + } + return ret; + } +}; \ No newline at end of file From 6d94a29e091f6508285c4234934b9ef4592765c6 Mon Sep 17 00:00:00 2001 From: ruitaocai Date: Sun, 9 Aug 2015 15:29:02 +0800 Subject: [PATCH 2/2] signture error --- PermutationsII.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PermutationsII.h b/PermutationsII.h index 9619344..898d23d 100644 --- a/PermutationsII.h +++ b/PermutationsII.h @@ -56,7 +56,7 @@ class Solution { class Solution2 { public: //return all possible unique permutations. - vector> permute(vector& nums) { + vector> permuteUnique(vector& nums) { sort(nums.begin(), nums.end());//!!! vector> ret; ret.push_back(nums);