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 ...
随机推荐
- Mac 下配置Lua环境
1.下载LUA源码包 curl -R -O http://www.lua.org/ftp/lua-5.3.1.tar.gz 2.解压并进入目录 tar -xvf lua-.tar.gz cd lua- ...
- 团队项目(MVP------新能源无线充电管理网站)(个人任务3)
现在我们组的项目已经完成了,之前做的欢迎界面已经废弃掉了,于是我重新制作了一个欢迎界面,主要是分为了团队介绍,充电商品的介绍,现在充电新闻的发展,解决方案,成功案例.其中产品里面又有两个商品的售卖页, ...
- wpf UI 布局
1.Grid *号代表百分比,也可以使用固定值,需要预先设置 有几行几列 以及宽度和高度 ,在表格中的控件需要 表明自己所在第几行 第几列 2.StackPanel 重点需要设置 排列方向 水平还是垂 ...
- datatables 行与列的数据获取
datatables官网: https://datatables.net/reference/api/cells() 获取数据的方式如下 var table = $(selector).DataTab ...
- ecmall 学习记录2
1.ecmall 自带的写入日志方法:do_log4php("函数名","类名",$param); $param是参数 在类里调用写入之日的方法 需要先加载 ...
- django中利用FastDFS来上传图片的流程
好处:海量问题,存储容量扩展方便,文件内容重复并且对静态文件的访问也有了提升等.
- matplotlib画sin(x)和cos(x)/2
import matplotlib.pyplot as mp 1. 基本的绘图 mp.plot(水平坐标, 垂直坐标, linestyle=线型, linewidth=线宽, color=颜色, .. ...
- Python全栈之路----函数----内置方法
Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod ...
- hdu 1754 I Hate It (单点修改+区间最值+裸题)
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 《DSP using MATLAB》Problem 7.6
代码: 子函数ampl_res function [Hr,w,P,L] = ampl_res(h); % % function [Hr,w,P,L] = Ampl_res(h) % Computes ...