46. Permutations (JAVA)
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)的更多相关文章
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- 46. Permutations
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- Gradle 1.12用户指南翻译——第46章. Java 库发布插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- 46. Permutations 排列数
46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...
- 刷题46. Permutations
一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- Permutations java实现
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
随机推荐
- vue中的methods,conputed,watcher
todo 1,computed(计算属性) 属性的结果会被缓存, 除非依赖的响应式属性变化才会重新计算, 主要 当做属性来使用;(虽然是个方法,但是当做属性使用)(场景:引用一些经过操作的数据) co ...
- 安装浏览器的vue插件
安装浏览器的vue插件步骤: 1.在浏览器中打开 https://github.com/vuejs/vue-devtools Clone or download vue-devtools. 2.解压 ...
- IView入门练习~CDN模式全局加载JS
关于 iView iView 是一套基于 Vue.js 的开源 UI 组件库,主要服务于 PC 界面的中后台产品. 特性 高质量.功能丰富 友好的 API ,自由灵活地使用空间 细致.漂亮的 UI 事 ...
- HDU 6625 (01字典树)
题意:给定两个长为n的数组a和b:重新排列a和b,生成数组c,c[i]=a[i] xor b[i]:输出字典序最小的c数组. 分析:将a中的数插入一颗01字典树a中:将b中的数插入一颗01字典树b中: ...
- leetcode-mid-sorting and searching -347. Top K Frequent Elements
mycode 71.43% class Solution(object): def topKFrequent(self, nums, k): """ :type nu ...
- 二、Python基础
1.变量名 数字,字母,下划线:aaa1;aa_b1 不能以数字开头:1aa 变量名不能是python内部的关键字 2.getpass import getpass username=raw_inpu ...
- UI-WPF_UI:WPF UI - DMSkin官方网站
ylbtech-UI-WPF_UI:WPF UI - DMSkin官方网站 1.返回顶部 1. QQ: 944095635 DMSkin.com 首页 论坛 定制 博客 联系 DMSkin 3. ...
- Unity3D-Canvas-UIScaleMode
UnityCanvasUI自适应部分 用Canvas下的自适应UI Scale Mode: Constant Pixel Size:固定像素大小,无论如何UI怎么样就怎么样,只能通过 Scale Fa ...
- 【Spring】---【IOC入门案例】
第一步:导入Jar包 第二步:创建类,在类里面创建方法 public class User { public void add(){ System.out.println("-------- ...
- shell基础命令
什么是脚本? 脚本简单地说就是一条条的文字命令(一些指令的堆积),这些文字命令是可以看到的(如可以用记事本打开查看.编辑). 常见的脚本: JavaScript(JS,前端),VBScript, AS ...