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. ubuntu 使用cron设置定时启动任务

    介绍 cron,是一个Linux定时执行工具,可以在无需人工干预的情况下运行作业. 在Ubuntu server 下,cron是被默认安装并启动的:如果没有启动,自行设置并启动(chkconfig\s ...

  2. resful

    一个完整的node resful api 一个更完整的项目 介绍

  3. @Configuration的使用

    以下内容转载自:duanxz的spring4.0之二:@Configuration的使用,如有侵权,请联系作者本人予以删除 从Spring3.0,@Configuration用于定义配置类,可替换xm ...

  4. APPlication,Session和Cookie的区别

    方法 信息量大小 保存时间 应用范围 保存位置 Application 任意大小 整个应用程序的生命期 所有用户 服务器端 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟 ...

  5. Python之小练习

    1.1 2 3 4 5 6 7 8能组成多少个不同的两位数? count = 0for i in range(1,9): for V in range(1,9): if i != V: count+= ...

  6. 使用loadrunner监控apcahe

    一般要修改的内容在Httpd.conf文件中已经存在,如果不存在请自行添加相应内容. (1)修改Apache中Httpd.conf文件, (2)添加ExtendedStatus,设置ExtendedS ...

  7. PythonStudy——进制 System of numeration

    十进制 人类天然选择了十进制. 二进制 二进制有两个特点:它由两个数码0,1组成,二进制数运算规律是逢二进一. 四进制 四进制是以4为基数的进位制,以 0.1.2 和 3 四个数字表示任何实数. 七进 ...

  8. Office常用技巧

    文章目录 大小写切换 把word里的自动编号转换为真实的文本 大小写切换 word中修改单词/句子的大小写:选中文字,按shift+F3,可在全大写.全小写.首字符大写间切换. 把word里的自动编号 ...

  9. 集合总结二(LinkedList的实现原理)

    一.概述 先来看看源码中的这一段注释,我们先尝试从中提取一些信息: Doubly-linked list implementation of the List and Deque interfaces ...

  10. python unittest setUp 和 setUpClass 区别

    import unittest class Test(unittest.TestCase): def setUp(self): print("start!=======") def ...