LeetCode46,47 Permutations, Permutations II

题目:

LeetCode46 I

Given a collection of distinct numbers, return all possible permutations. (Medium)

For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

LeetCode47 II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.(Medium)

For example,
[1,1,2] have the following unique permutations:

[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

分析: 首先先用next_permutation解这两个题。用好STL。

Permutations I:

 1 class Solution {
 2 public:
 3     vector<vector<int>> permute(vector<int>& nums) {
 4         vector<vector<int>> result;
 5         int len = nums.size();
 6         sort(nums.begin(), nums.end());
 7         do {
 8             result.push_back(nums);
 9         } while (next_permutation(nums.begin(),nums.end()));
10
11         return result;
12     }
13 };

Permutations II:

 1 class Solution {
 2 public:
 3     vector<vector<int>> permuteUnique(vector<int>& nums) {
 4         vector<vector<int>> result;
 5         int len = nums.size();
 6         sort(nums.begin(), nums.end());
 7         do {
 8             result.push_back(nums);
 9         } while (next_permutation(nums.begin(),nums.end()));
10
11         return result;
12     }
13 };

当然,用STL写好的函数肯定不是面试的目的,permutation的递归方法也是经典的回溯算法题。

代码:

 1 class Solution {
 2 private:
 3     vector<vector<int>> result;
 4     void helper(vector<int>& nums, int start, int end) {
 5         if (start == end) {
 6             result.push_back(nums);
 7         }
 8         for (int i = start; i <= end; ++i) {
 9             swap(nums[start], nums[i]);
10             helper(nums, start + 1, end);
11             swap(nums[start], nums[i]);
12         }
13     }
14 public:
15     vector<vector<int>> permute(vector<int>& nums) {
16         helper(nums, 0, nums.size() - 1);
17         return result;
18     }
19 };
时间: 2024-10-15 10:25:15

LeetCode46,47 Permutations, Permutations II的相关文章

[LeetCode 46 &amp; 47] Permutations I &amp; II

题目链接:permutations 相似题型: 1. [LeetCode 39&40] Combination Sum I & II 2. [LeetCode 78] Subsets 3. [LeetCode 90] Subsets II 4. [LeetCode 22] Generate Parentheses 5. [LeetCode 77] Combinations import java.util.ArrayList; import java.util.List; /** * Gi

leetcode || 47、 Permutations II

problem: 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]. Hide Tags Backtracking 题意:给定一个带重复数字的序列,输出其所有的排列

Permutations I &amp; II

I 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]. public class Solution { public ArrayList<ArrayList<Integer>> permu

[Leetcode]Permutations &amp;&amp; Permutations II

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]. 组合的题目,第一题无重复,第二题有重复. 自我感觉最常规的方法就是回溯法,两道题都试用.第一道没有重复的情况下还可以有更取巧的办法,第二题貌似只能

LeetCode (18) Permutations I &amp; II (排列一、二)

不存在重复的情况:题目描述 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]. 本题要求输入一数组,输出数组的所有排列方式.题目中给定的为int类型的数组,有些题目中给定的为字符类型的,两种思路是一

Permutations Permutations||

地址:https://oj.leetcode.com/problems/permutations/ https://oj.leetcode.com/problems/permutations-ii/ 题目简单说就是实现全排列.为了实现这个要求首先进行一下观察. 用123来示例下.123的全排列有123.132.213.231.312.321这六种.首先考虑213和321这二个数是如何得出的.显然这二个都是123中的1与后面两数交换得到的.然后可以将123的第二个数和每三个数交换得到132.同理可

[C++]LeetCode: 120 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]. 思路:这道题和Permutations的区别就是,输入的数字数组中包含重复的元素.如果我们对重复的元素不

[GeeksForGeeks] Friends Pairing Problem

Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up. Examples: Input : n = 3 Output : 4

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(