【leetcode】Permutations
题目描述:
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2]
, and [3,2,1]
.
解题思路:
这道题目由于是求所有的全排列,比较直观的方法就是递归了
class Solution:
# @param num, a list of integer
# @return a list of lists of integers
def permute(self,l):
if(len(l)<=1):
return [l]
r=[]
for i in range(len(l)):
s=l[:i]+l[i+1:]
p=self.permute(s)
for x in p:
r.append(l[i:i+1]+x)
return r
s = Solution()
print s.permute([1,2,3])
C++版
class Solution {
public:
void permutation(vector<vector<int>>& res, vector<int>& nums, int depth) {
if(depth==nums.size()-1) res.push_back(nums);
for(int i=depth; i < nums.size(); ++i){
swap(nums[i], nums[depth]);
permutation(res, nums, depth+1);
swap(nums[i], nums[depth]);
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
permutation(res, nums, 0);
return res;
}
};
【leetcode】Permutations的更多相关文章
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【leetcode】Permutations (middle)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【leetcode】Permutations II (middle)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- Git初级实践教程(图文)
关于Git Git的由来 Linux 的创始人 Linus Torvalds 在 2005 年开发了 Git 的原型程序.当时,由于在 Linux 内核开发中使用的既有版本管理系统的开发方许可证发生了 ...
- HTTP请求头
了解HTTP请求,是每个BS程序员必备的素质.下面篇幅进行记录.参考网址:http://tools.jb51.net/table/http_header Http请求方式 GET: 向Web服务器请求 ...
- php 实用例子:购物车 涉及session与ajax
login: <div>用户名:<input type="text" id="uid" /></div><div> ...
- python Windows下的android设备截图工具
目录 界面版 命令行版 界面版 利用python的wx库写个ui界面,用来把android设备的截图输出到电脑屏幕,前提需要安装adb,涉及到的python库也要安装.代码如下: #!/usr/bin ...
- Angular2.0快速开始
参考资料: Angular2.0快速开始 AngularJS2 教程
- codevs3250 操作序列
题目描述 Description Petya是一个非常好玩孩子.他很无聊,因此他开始玩下面的游戏: 他得到一个长度为N的整数序列,他会对这些数字进行操作,他可以把某个数的数值加1或者减1(当然他可以对 ...
- MVC验证session是否过期,在每个action执行之前验证
protected override void OnActionExecuting(ActionExecutingContext filterContext) { ...
- PHP正则表达式模式修饰符详解
PHP模式修饰符又叫模式修正符,是在正则表达式的定界符之外使用.主要用来调整正则表达式的解释,提扩展了正则表达式在匹配.替换等操作的某些功能,增强了正则的能力.但是有很多地方的解释都是错误的,也容易误 ...
- thinkphp自动验证---$_validate
thinkphp中的自动验证 array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间]) 1.验证字段 需要验证的表单字段名称,这个字段不一定是数据库字段,也可以是表单的一些辅助字段 ...
- BZOJ 4581: [Usaco2016 Open]Field Reduction
Description 有 \(n\) 个点,删掉三个点后,求最小能围住的面积. Sol 搜索. 找出 左边/右边/上边/下边 的几个点枚举就可以了. 我找了 12 个点,统计一下坐标的个数,然后找到 ...