[Leetcode 46]全排列 Permutations 递归
【题目】
Given a collection of distinct integers, return all possible permutations.
数组的组合情况。
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
【思路】
求子集,排列组合的数组题有固定模板。
【代码】
class Solution {
public List<List<Integer>> permute(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ans=new ArrayList<>();
List<Integer> tmp=new ArrayList<>();
fun(ans,tmp,nums);
return ans;
}
public void fun(List<List<Integer>> ans,List<Integer> tmp,int[] nums){
if(tmp.size()==nums.length){
ans.add(new ArrayList<>(tmp));
}
else{
for(int i=0;i<nums.length;i++){
if(tmp.contains(nums[i]))
continue;
tmp.add(nums[i]);
fun(ans,tmp,nums);
tmp.remove(tmp.size()-1);
}
}
}
}
[Leetcode 46]全排列 Permutations 递归的更多相关文章
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- Java实现 LeetCode 46 全排列
46. 全排列 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2] ...
- [leetcode] 46. 全排列(Java)
46. 全排列 这题我们可以借用31. 下一个排列写的nextPermutation函数来做,稍微改造一下即可 注意要先给nums排个序 class Solution { // 当没有下一个排列时re ...
- leetcode 46. 全排列 及 47. 全排列 II
46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...
- LeetCode 46. 全排列(Permutations)
题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [ ...
- [LeetCode] 46. 全排列(回溯)
###题目 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...
- LeetCode 46 全排列
题目: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3 ...
- LeetCode 46——全排列
1. 题目 2. 解答 给定一个序列,序列中的任意一个数字都可以作为全排列的最后一位.然后,其余位置元素的确定便是剩余元素的一个全排列,也就是一个子问题. 例子中 [1, 2, 3] 的全排列,最后一 ...
- leetcode 46 全排列 (python)
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] ...
随机推荐
- sublime lincense for linux
sublime lincense for linux Sublime Text 3.x (after Build 3133) —– BEGIN LICENSE —–TwitterInc200 User ...
- bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: lxml.
python3 bs4解析网页时报错: bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requeste ...
- vue mand-mobile ui Stepper步进器默认值传字符串进去不起作用
vue mand-mobile ui Stepper步进器默认值传字符串进去不起作用 Stepper 步进器 的默认值有没有弄过的,看了组件只能默认数字的,传字符串进去不起作用<div slot ...
- websocket 二合一
server from flask import Flask, request, render_template from geventwebsocket.handler import WebSock ...
- vue计算属性和侦听器
一.计算属性: main.js: var app = new Vue({ el: '#app', data: { math: 80, physics: 90, english: 30 }, compu ...
- Oarcle 入门之like关键字
like关键字 模糊查询,有两个特殊的符号"%" ,"_" “%”表示匹配零个或若干字符 放哪边就表示哪边有零到若干个未知符号,其作用起到占位符的效果. “_” ...
- 笔记02 linux的一些命令sed
#!/bin/bash # dataformat=`date +%Y-%m-%d-%H-%M` #进行文件件cp并重命名 nginx_home=/opt/modules/nginx-1.12/ cp ...
- 面向对象select方法
<?php class Table{ protected $tablename; protected $arrTable; protected $w ...
- Netbeans and Remote Host for C/C++ Developing
Netbeans and Remote Host for C/C++ Developing 很久以来,因为我不适应在 Linux 下使用 Vim, GCC, GDB 开发 C/C++ 程序,所以我一直 ...
- 03:CDN原理
1.1 CDN简介 1.CDN作用(缓存静态资源) 1. CDN的全称Content Delivery Network,(缩写:CDN)即内容分发网络. 2. CDN解决由于网络带宽小.用户访问量大. ...