Given a collection of distinct integers, return all possible permutations.

Example:

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) {
List<Integer> ans = new ArrayList<Integer>();
if(nums.length == 0) return ret; ans.add(nums[0]);
insertNum(nums, 1, ans);
return ret;
} public void insertNum(int[] nums, int index, List<Integer> ans){
if(index == nums.length) {
List<Integer> new_ans = new ArrayList<Integer>(ans);
ret.add(new_ans);
return;
} //insert in the back
ans.add(nums[index]);
insertNum(nums, index+1, ans);
ans.remove(ans.size()-1); //recover for(int j = 0; j < ans.size(); j++){ //iterate all possible insert position
ans.add(j,nums[index]);
insertNum(nums, index+1, ans);
ans.remove(j); //recover
}
} private List<List<Integer>> ret = new ArrayList<List<Integer>>();
}

46. Permutations (JAVA)的更多相关文章

  1. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  2. 46. Permutations

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  3. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  4. Gradle 1.12用户指南翻译——第46章. Java 库发布插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  5. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...

  6. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  7. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  8. Permutations java实现

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

随机推荐

  1. sqli-labs(19)

    百度了一下 基于错误的referer头的注入 0X01爱之初体验 猜测是基于referer头的注入 我们在referer后面加入单引号测试一下 真的报错了诶 那我们猜测一下 他应该是把 referer ...

  2. How jQuery UI Works

    https://learn.jquery.com/jquery-ui/how-jquery-ui-works/ jQuery UI contains many widgets that maintai ...

  3. 【洛谷T89379 【qbxt】复读警告】

    题目链接 这个题可以应用dp #include<bits/stdc++.h> using namespace std; ; inline int read() { ,b=; char c= ...

  4. OpenCV学习笔记(1)

    一.读入图像 使用cv2.imread()读入图像,图像应该在此程序的工作路径,第二个参数是告诉函数应该如何读取这幅图片 cv2.IMREAD_COLOR:读入一副彩色图像.图像的透明度会被忽略,这是 ...

  5. [VBA]删除多余工作表

    sub 删除多余工作表() Dim i As Integer Application.DisplayAlerts = False For i = Worksheets.Count To 1 step ...

  6. C# 截获某个域中未捕获的异常 CLR20R3 程序终止的几种解决方案

    AppDomain.UnhandledException可以获的异常,却截不下来,求解 AppDomain.CurrentDomain.UnhandledException += CurrentDom ...

  7. apache-httpd2.4编译安装

    centos 6 编译安装httpd-2.4 centos6yum安装的apr版本已经不适用httpd-2.4版本了.所以,需要源码编译apr以及apr-util1. 下载源码:cd /usr/loc ...

  8. zabbix+grafana使用

    参照文档 https://blog.csdn.net/xiegh2014/article/details/54928883

  9. MAVEN打包时跳过Junit测试

    我们知道,通常情况下使用maven package命令打包时,会自动执行test包下的各个单元测试. 这是因为spring-boot-maven-plugin插件已经集成了maven-surefire ...

  10. 整理一下go的ci工具

    代码格式化 go fmt fileName.go goimports 自动格式化import goimports -w fileName.go mod 自动更新/删除包 go mod tidy 检查注 ...