【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 ...
随机推荐
- Maven随记
如何保持依赖的多个jar保持版本一致 在引入依赖的时候常常需要依赖多个独立的模块, 譬如Spring的content, aop等等, 为了保持版本一致, 可以设置<spring.version& ...
- 教你一招:根据身份证号计算出生年月和年龄 text函数和mid函数混用 datedif函数和today函数混用
在电子表格Excel中,使用text函数和mid函数混用,根据身份证号计算出生年月: =text(mid(A2,,),"0!/00!/00") #0!/00!/00 为日期的格式# ...
- Zabbix使用点滴
Application --> Items --> Triggers 1.磁盘柜日志输出至Rsyslog服务器,Zabbix抓取:System-Syslogsystem-log(log[/ ...
- SSH--1
package com.etc.action; import java.io.IOException; import java.io.PrintWriter; import java.util.Has ...
- (转)Javascript本地存储小结
转自:https://i.cnblogs.com/EditPosts.aspx?opt=1 以下是原文: 1. 各种存储方案的简单对比 Cookies:浏览器均支持,容量为4KB UserData:仅 ...
- [NHibernate]HQL查询
目录 写在前面 文档与系列文章 查询的几种方式 HQL查询 一个例子 总结 写在前面 上篇文章介绍了nhibernate在项目中的基本配置,包括数据库连接字符串的设置,映射文件的配置及需注意的地方,这 ...
- HTML5+CSS3 - 代码简写篇
有话先说:我是一只菜鸟,还是一只刚步入前端这个领域的小菜年,在不断的进阶,理解最深刻的还是代码,既爱又恨却不知如何去感悟. background属性简写: background-position属性组 ...
- I() 方法
I()方法的介绍及使用: http://www.jb51.net/article/51213.htm
- Effective Java Second Edition --- Builder Pattern
如果类的构造器或者静态工厂中有多个参数,设计这种类时,Builder模式是一种不错的选择,特别是当大多数参数是可选的时候. 与使用传统的重载构造函数模式相比,使用Builder模式的客户端代码更易于阅 ...
- PHP合并2个数字键数组的值
先要了解一个基础知识点:PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧 <?php /** * PHP合并2个数字键数组的值 * * @param arr ...