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. go web framework gin 启动流程分析

    最主要的package : gin 最主要的struct: Engine Engine 是整个framework的实例,它包含了muxer, middleware, configuration set ...

  2. Linux:CentOS 7系统的安装

    相信有看过我写的博文就知道我写的第一篇博文就是CentOS 7系统的安装,不过是在虚拟机中安装的,而且还是直接加载镜像文件进去的,不过这次我就通过PE来安装,来证实下PE是否可以用来安装Linux系统 ...

  3. winfrom 关闭别的应用程序的窗体或者弹出框(winform 关闭句柄)

    在word转换成html的时候,由于系统版本不一样,office总是抛出异常,Microsoft Word停止工作,下面有三个按钮,关闭程序等等,但是我的转换工作需要自动的,每当抛出异常的时候我的程序 ...

  4. 9--Python入门--模块

    模块简单来说是一个保存了python代码的文件很多python开源库就是模块 #例如我们调用科学计算库 numpy import numpy as np #as np是为了之后方便调用 from pa ...

  5. Android开发中Activity状态的保存与恢复

    当置于后台的Activity因内存紧张被系统自动回收的时候,再次启动它的话他会重新调用onCretae()从而丢失了之前置于后台前的状态. 这时候就要重写Activity的两个方法来保存和恢复状态,具 ...

  6. idea快捷键的设置

    因为开始学java被培训机构里的无良老师给带偏,"染上了"MyEclipse的快捷键"恶习",于是很难改了. 所以,在使用idea时,要设置快捷键了,快点适应之 ...

  7. 启动tomcat报错Status 状态为 Deployment failed

  8. Ubuntu16.04安装YouCompleteMe

    1.要求vim的版本在7.4.143以上,支持python2/3,通过vim --version查看. 2.下载源码:  https://github.com/Valloric/YouComplete ...

  9. GoStudy——Go语言入门第一个事例程序:HelloWorld.go

    package main import ( ​ "fmt" ) func main() { ​ fmt.Println("Hello,world!!!--2019年4月1 ...

  10. 关于动态内存malloc和realloc

    1.malloc   1.申请的内存长度可以运行时决定,单位是字节  2.申请的内存为连续的内存空间  3.返回的地址可以根据实际需要强转成对应的类型  4.动态申请内存的生命周期是整个程序,除非手动 ...