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]
]

题解

该题是求所有可能的排列组合,是一道典型的dfs类型的题

代码(python实现)

import copy
class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if nums is None:
            return
        results = []
        if len(nums) == 0:
            return results
        self.helper(nums, results, [])
        return results
    def helper(self, nums, results, list):

        if len(list) == len(nums):
            #注意此处是深度拷贝,如果用java语言写则以list为输入new出一个新的实例
            results.append(copy.deepcopy(list))
            return

        for i in range(len(nums)):
            if(nums[i] in list):
                continue

            list.append(nums[i])
            self.helper(nums, results, list)
            list.pop()
时间: 2024-12-02 16:51:46

46. Permutations的相关文章

<LeetCode OJ> 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,

[LeedCode OJ]#46 Permutations

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/permutations/ 题意: 给定一个数组,要求返回其所有全排列的情况 思路: 对于一个特定排列我们有一个求下一个全排列的函数,那就是next_permutation,运用这个函数这道题迎刃而解 class Solution { public: vector<vector<int> > permute(

LeetCode OJ: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]. 排列组合的问题,不用考虑是否发生重复的情况,代码如下: 1 class Solution { 2 public: 3 vector<vector<i

白菜刷LeetCode记-46. Permutations

今天这一题也是中等难度,题目如下: 这一题是要实现数组的全排列.这一题是要使用遍历以及递归的思想去实现,代码如下: 1 /** 2 * @param {number[]} nums 3 * @return {number[][]} 4 */ 5 var permute = function(nums) { 6 var res = new Array(); 7 8 helper(nums, 0, nums.length - 1 , res); 9 10 return res; 11 }; 12 1

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

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为最初的

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] ] 题目大意:求一个序列的全排列. 思路:做排列

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/ 题目大意:求全排列 题目分析:求

[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]

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar