leetcode46
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的更多相关文章
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- LeetCode46 回溯算法求全排列,这次是真全排列
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode的26篇文章,我们来实战一下全排列问题. 在之前的文章当中,我们讲过八皇后.回溯法,也提到了全排列,但是毕竟没有真正写 ...
- [Swift]LeetCode46. 全排列 | Permutations
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- LeetCode46. Permutations
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- Leetcode46. Permutations全排列
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LeetCode 47. 全排列 II(Permutations II)
题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...
随机推荐
- MySQL免安装版错误解决方案
在进行mysql zip版本的安装时,遇到如下的错误 一.提示 无法启动此程序,丢失 msvcr140.dll 解决方法 下载Visual C++ Redistributable for Visual ...
- react 调用webIm
记录下遇到的问题,之前引用腾讯云的webim,一直出错,现在改好了, 引用了, 以上是在public下的index.html引用, 但是在子模块console.log(webim);会报这个错 解决也 ...
- windows环境安装MySQL
转:https://www.cnblogs.com/ayyl/p/5978418.html windows环境安装MySQL mySQL下载链接:MySQL Installer 5.7 :http:/ ...
- sqlserver查询当前库下,一张表的表名,字段名,字段类型,字段长度
sqlserver版: 查询当前数据库下所有表名: select * from sys.tables; 查询当前库下,一张表的表名,字段名,字段类型,字段长度: select a.name 表名,b. ...
- j教你如何用erlang-tuple
元组是用来保存一组数据元素的复合数据类型,其中数据元素是要求为erlang的数据类型,单不一定要是相同的类型,元组使用封闭的花括号{}来定义,里面的元素有逗号隔开. 例如: 1> {1,2,3} ...
- 《ProgrammingHive》阅读笔记-第二章
书本第二章的一些知识点,在cloudera-quickstart-vm-5.8.0-0上进行操作. 配置文件 配置在/etc/hive/conf/hive-site.xml文件里面,采用mysql作为 ...
- maven项目pom.xml第一行报错
maven项目pom.xml第一行报错 这是第一行:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= ...
- CPU瓶颈分析工具
性能指标: 一.CPU利用率. 1.用户CPU使用率:用户态CPU使用率(user)和低优先级用户态CPU使用率(nice). 2.系统CPU使用率:说明内核比较忙. 3.等待I/O的CPU使用率(i ...
- 编译安装和apt安装Nginx1.14.0
安装依赖 yum -y install gcc gcc-c++yum -y install zlib zlib-devel openssl openssl-devel pcre-devel 在Ubun ...
- 《DSP using MATLAB》Problem 7.13
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...