1. public class Solution {
  2. public IList<IList<int>> Permute(int[] nums)
  3. {
  4. IList<IList<int>> result = new List<IList<int>>();
  5. permute(result, nums, );
  6. return result;
  7. }
  8.  
  9. private void permute(IList<IList<int>> result, int[] array, int start)
  10. {
  11. if (start >= array.Length)
  12. {
  13. List<int> current = new List<int>();
  14. foreach (int a in array)
  15. {
  16. current.Add(a);
  17. }
  18. result.Add(current);
  19. }
  20. else
  21. {
  22. for (int i = start; i < array.Length; i++)
  23. {
  24. swap(array, start, i);
  25. permute(result, array, start + );
  26. swap(array, start, i);
  27. }
  28. }
  29. }
  30.  
  31. private void swap(int[] array, int i, int j)
  32. {
  33. int temp = array[i];
  34. array[i] = array[j];
  35. array[j] = temp;
  36. }
  37. }

https://leetcode.com/problems/permutations/#/solutions

经过学习和思考,采用另一种写法实现。更容易理解

  1. public class Solution
  2. {
  3. List<IList<int>> list = new List<IList<int>>();
  4. int N;
  5. private void BackTrack(List<int> records, List<int> save, int t)
  6. {
  7. if (t >= N)
  8. {
  9. var temp = new int[N];
  10. save.CopyTo(temp);
  11. list.Add(temp.ToList());
  12. return;
  13. }
  14.  
  15. for (int i = ; i < records.Count; i++)
  16. {
  17. var current = records[i];
  18. save.Add(current);
  19. var notsave = records.Where(x => x != current).ToList();
  20. BackTrack(notsave, save, t + );
  21. save.Remove(current);
  22. }
  23. }
  24.  
  25. public IList<IList<int>> Permute(int[] nums)
  26. {
  27. N = nums.Length;//初始化最大范围
  28. var records = nums.ToList();
  29. var save = new List<int>();
  30. BackTrack(records, save, );
  31. return list;
  32. }
  33. }

补充一个python的实现:

  1. class Solution:
  2. def dfs(self,nums,n,path,l,visited):
  3. if len(path) == n:
  4. l.append(path[:])
  5. else:
  6. if nums != None:
  7. for i in range(len(nums)):
  8. if visited[i] == :
  9. continue
  10. path.append(nums[i])
  11. visited[i] =
  12. self.dfs(nums,n,path,l,visited)
  13. visited[i] =
  14. path.pop(-)
  15.  
  16. def permute(self, nums: 'List[int]') -> 'List[List[int]]':
  17. n = len(nums)
  18. path = list()
  19. l = list()
  20. visited = [] * n
  21. self.dfs(nums,n,path,l,visited)
  22. return l

leetcode46的更多相关文章

  1. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  2. LeetCode46 回溯算法求全排列,这次是真全排列

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode的26篇文章,我们来实战一下全排列问题. 在之前的文章当中,我们讲过八皇后.回溯法,也提到了全排列,但是毕竟没有真正写 ...

  3. [Swift]LeetCode46. 全排列 | Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  4. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  5. LeetCode46. Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  6. Leetcode46. Permutations全排列

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...

  7. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  8. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  9. LeetCode 47. 全排列 II(Permutations II)

    题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...

随机推荐

  1. Codeforces Round #324 (Div. 2) (哥德巴赫猜想)

    题目:http://codeforces.com/problemset/problem/584/D 思路: 关于偶数的哥德巴赫猜想:任一大于2的偶数都可写成两个素数之和. 关于奇数的哥德巴赫猜想:任一 ...

  2. servlet邮箱激活验证实例含代码

    也有很多人本来有机会的,他们都拒绝了,不想让自己太累,太麻烦.或者中途被情绪所左右,半途而废了. 成长是有代价的,同样悠闲也是有代价的. 流程: 用户填写相关信息,点击注册按钮 系统先将用户记录保存到 ...

  3. 结构体变量的 extern 使用方法,转--

    要求如下,在.h文件中这样定义: typedef struct typFNT_GB16     // 汉字字模数据结构 {     signed ];        // 汉字内码索引     ]; ...

  4. 外网访问SQLServer数据库holer实现

    外网访问内网SQLServer数据库 内网主机上安装了SQLServer数据库,只能在局域网内访问,怎样从公网也能访问本地SQLServer数据库? 本文将介绍使用holer实现的具体步骤. 1. 准 ...

  5. 全文检索的Demo

    用到lucene版本为6.3.0版本,利用的分词器为IKAnalyzer分词器,该分词对中文有较好的支持.关于支持lucene的6.xx以上的IkAnalyzer分词jar包下载地址:https:// ...

  6. JS实现数组的相减操作

    /** * 数组相减的方法 * @param {Array} a * @param {Array} b */ function arrSubtraction(a, b) { if (!a || !b ...

  7. 蓝牙协议分析(4)_IPv6 Over BLE介绍

    1. 前言 蓝牙是个奇葩的家伙:它总是以后来者的身份出现,很喜欢打仗,而且还不落下风(有点像某讯的风格).90年代末期和Wi-Fi的无线标准之争如此,当前和802.15.4系(ZigBee.RF4CE ...

  8. Windows 应用商店无法下载---启动更新

    今天想在应用商店下载东西,但是以直没成功,查看原因结果是因为我的Windows自动更新关了. 百度,如何打开自动更新,要打开本地组策略编辑器,但是我是Windows家庭版,,,没有这个东西,, 最后, ...

  9. 源代码安装-非ROOT用户安装软件的方法

    0.    前言 如果你没有sudo权限,则很多程序是无法使用别人编译好的文件安装的. 还有时候,没有对应你的主机配置的安装包,这时候需要我们自己下载最原始的源代码,然后进行编译安装. 这样安装的程序 ...

  10. JavaStudy——Java之自动拆箱与自动装箱

    java基本类型介绍 java中,基本数据类型一共有8种,详细信息如下表: 类型 大小 范围 默认值 byte 8 -128 - 127 0 short 16 -32768 - 32768 0 int ...