46 Permutations(全排列Medium)

题目意思:全排列

思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧

  ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到12ms

 1 class Solution {
 2 public:
 3     vector<vector<int>> permute(vector<int>& nums) {
 4         vector<vector<int> >ans;
 5         permute1(ans,nums,0);
 6         return ans;
 7     }
 8     void permute1(vector<vector<int>>& ans,vector<int>& nums,int begin) {
 9         if(begin==nums.size()-1){
10             ans.push_back(nums);
11         }
12         for(int i=begin;i<nums.size();++i){
13             swap(nums[i],nums[begin]);
14             permute1(ans,nums,begin+1);
15             swap(nums[i],nums[begin]);
16         }
17     }
18 };
时间: 2024-08-07 17:01:35

46 Permutations(全排列Medium)的相关文章

46. Permutations (全排列)

Given a collection of distinct 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], [3,2,1] ] 运用递归. 1234为例子 for  i in 1234: 1  +  234(的全排列) 2  +  134(的全排列) 3  +

LeetCode 46 Permutations (全排列)

Given a collection of distinct 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], [3,2,1] ] 题目链接:https://leetcode.com/problems/permutations/ 题目大意:求全排列 题目分析:求

leetCode 46. Permutations 回溯问题 | Medium

46. Permutations(全排列问题--回溯问题经典) Given a collection of distinct 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],   [3,2,1] ] 题目大意:求一个序列的全排列. 思路:做排列

&lt;LeetCode OJ&gt; 46. Permutations

46. Permutations My Submissions Question Total Accepted: 81495 Total Submissions: 239854 Difficulty: Medium Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,

[Lintcode]15. Permutations/[Leetcode]46. Permutations

15. Permutations/46. Permutations 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers, return all possible permutations. Example Example 1: Input: [1] Output: [ [1]] Example 2: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3]

刷题46. Permutations

一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解: 刷题31. Next Permutation 我考虑可以用dp做,写了一个上午,理论我就不说了,自己看代码: #include<iostream> #include<vector> #include<unordered_map> using namespace std;

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1

46. Permutations 排列数

46. Permutations 题目 Given a collection of distinct 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], [3,2,1] ] 解析 class Solution_46 { public: void help(int

LeetCode 46 Permutations(全排列问题)

题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组能够产生的所有全排列 采用递归算法,传入参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列 nums为最初的