public class Solution {
public IList<IList<int>> Permute(int[] nums)
{
IList<IList<int>> result = new List<IList<int>>();
permute(result, nums, );
return result;
} private void permute(IList<IList<int>> result, int[] array, int start)
{
if (start >= array.Length)
{
List<int> current = new List<int>();
foreach (int a in array)
{
current.Add(a);
}
result.Add(current);
}
else
{
for (int i = start; i < array.Length; i++)
{
swap(array, start, i);
permute(result, array, start + );
swap(array, start, i);
}
}
} private void swap(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

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

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

public class Solution
{
List<IList<int>> list = new List<IList<int>>();
int N;
private void BackTrack(List<int> records, List<int> save, int t)
{
if (t >= N)
{
var temp = new int[N];
save.CopyTo(temp);
list.Add(temp.ToList());
return;
} for (int i = ; i < records.Count; i++)
{
var current = records[i];
save.Add(current);
var notsave = records.Where(x => x != current).ToList();
BackTrack(notsave, save, t + );
save.Remove(current);
}
} public IList<IList<int>> Permute(int[] nums)
{
N = nums.Length;//初始化最大范围
var records = nums.ToList();
var save = new List<int>();
BackTrack(records, save, );
return list;
}
}

补充一个python的实现:

 class Solution:
def dfs(self,nums,n,path,l,visited):
if len(path) == n:
l.append(path[:])
else:
if nums != None:
for i in range(len(nums)):
if visited[i] == :
continue
path.append(nums[i])
visited[i] =
self.dfs(nums,n,path,l,visited)
visited[i] =
path.pop(-) def permute(self, nums: 'List[int]') -> 'List[List[int]]':
n = len(nums)
path = list()
l = list()
visited = [] * n
self.dfs(nums,n,path,l,visited)
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. numpy-随机数

    import numpy as np nr=np.random nr.seed(0) np.set_printoptions(precision=2) # 只显示小数点后2位 print(nr.ran ...

  2. RabbitMQ 的安装----windows环境

    一.RabbitMQ在windows下的安装 RabbitMQ 它依赖于Erlang,在window上安装时,需要先安装Erlang. 首先确定你的window电脑是32位还是64位,然后下载对应版本 ...

  3. JavaScript 注意要点

    何时加引号: 只有变量不加引号.加了引号的一定不是变量,是字符 方法:     方法一律带有小括号 js 中的作用域    全局变量: 在最外层定义的变量: 在函数体内部看是没有声明var的 也是全局 ...

  4. RSA加密解密实现(JAVA)

    1.关于RSA算法的原理解析参考:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html 2.RSA密钥长度.明文长度和密 ...

  5. Linux下初次使用github

    1.安装 1.1 使用yum安装的 命令:$ yum install git git-gui 1.2 生成密钥对,使用ssh-keygen方法 ssh-keygen -t [rsa|dsa],将会生成 ...

  6. java JDBC编程流程步骤

    JDBC:Java Data Base Connection JDBC是用于运行sql语句并从数据库中获取新新的java API. JDBC是用来(让我们的程序)通过网络来操作数据库的,作用非常重要: ...

  7. C与指针练习题4.14.1

    //C与指针练习题4.14.1 //ai+1=(ai+n/ai)/2公式逼近,当ai+1=ai时,取得n的平方根 #include<stdio.h> float sq_root(float ...

  8. Python重新安装pip

    1). 下载pip安装工具: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 2).执行升级命令: python2.7 get-pip. ...

  9. CCF-Markdown-201703-3

    这道题不存在递归结构 比如区块之间的相互嵌套 还有"[ [] ]" 链接的相互嵌套, 所以直接处理就好了 还可以 #include <bits/stdc++.h> us ...

  10. Spring Boot Shiro 权限管理 【转】

    http://blog.csdn.net/catoop/article/details/50520958 主要用于备忘 本来是打算接着写关于数据库方面,集成MyBatis的,刚好赶上朋友问到Shiro ...