全排列

题目描述:给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/permutations/

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:暴力破解法

用一个队列temp记录暂存的结果,每次遍历从队列中取出一个结果,然后往list中添加一个nums中的元素,其中要判断要添加的元素是否已经存在,如果存在,则重复了,不添加;如果不存在,则添加到队列中作为其中一个可能的结果。直到所有的list中的元素个数都是nums.length,返回所有的结果。

import java.util.*;

public class LeetCode_046 {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
int count = 0;
List<Integer> list = new ArrayList<>();
Queue<List<Integer>> temp = new LinkedList<>();
temp.add(list);
while (count < nums.length) {
int times = temp.size();
while (times > 0) {
List<Integer> cur = temp.poll();
for (int num : nums) {
List<Integer> next = new ArrayList<Integer>(Arrays.asList(new Integer[cur.size()]));
Collections.copy(next, cur);
if (!next.contains(num)) {
next.add(num);
temp.add(next);
}
}
times--;
}
count++;
}
result.addAll(temp);
return result;
} public static void main(String[] args) {
int[] nums = new int[]{1, 2};
for (List<Integer> integers : permute(nums)) {
for (Integer integer : integers) {
System.out.print(integer + " ");
}
System.out.println();
}
}
}

【每日寄语】 每天醒来将微笑别在衣襟就会遇见更多的美好。

LeetCode-046-全排列的更多相关文章

  1. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  2. LeetCode:全排列【46】

    LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...

  3. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  4. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  5. LeetCode 046 Permutations 全排列

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

  6. [LeetCode] Permutations 全排列

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

  7. [LeetCode] 46. 全排列(回溯)

    ###题目 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...

  8. LeetCode 47 全排列II

    题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一 ...

  9. LeetCode 46 全排列

    题目: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3 ...

  10. LeetCode 47. 全排列 II(Permutations II)

    题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...

随机推荐

  1. springboot 分布式项目,子级项目各自的作用。

    一.分布式项目,需要使用maven搭建. 1.1 父级pro.xml module. <?xml version="1.0" encoding="UTF-8&quo ...

  2. 如何在pyqt中在实现无边框窗口的同时保留Windows窗口动画效果(一)

    无边框窗体的实现思路 在pyqt中只要 self.setWindowFlags(Qt.FramelessWindowHint) 就可以实现边框的去除,但是没了标题栏也意味着窗口大小无法改变.窗口无法拖 ...

  3. 详解git fetch与git pull的区别(实操)

    感谢原文作者:R-H-R 原文链接:https://blog.csdn.net/riddle1981/article/details/74938111 git fetch和git pull都可以将远端 ...

  4. smartimageview 的原理

    自定义的控件在布局文件中的引用都需要指定类的完整路径 1.自定义了一个MyImageview类继承了Imageview,添加三个构造方法     2.添加一个setImageUrl方法接受一个图片ur ...

  5. Ext原码学习之lang-Array.js

    // JavaScript Document //Array 方法 (function(){ var arrayPrototype = Array.prototype, slice = arrayPr ...

  6. spring5+Struts2+hibernate5

    一,加jar包 加入顺序,个人推荐,spring->struts2->hibernate spring的jar包:基本包共21个+用到的aspectj注解的包2个+日志包1个 ------ ...

  7. python篇第10天【While 循环语句】

    while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值 ...

  8. python的namespace的理解

    Python命名空间的本质   python中的名称空间是名称(标识符)到对象的映射. 具体来说,python为模块.函数.类.对象保存一个字典(__dict__),里面就是重名称到对象的映射. -- ...

  9. opencv笔记--stitching模块

    opencv 提供了全景图像拼接的所有实现,包括: 1)stitching 模块提供了图像拼接过程中所需要的基本元素,该模块主要依赖于 features2d 模块: 2)提供了 stitching_d ...

  10. Solution -「LOCAL」「cov. HDU 6864」找朋友

    \(\mathcal{Description}\)   Link.(几乎一致)   给定 \(n\) 个点 \(m\) 条边的仙人掌和起点 \(s\),边长度均为 \(1\).令 \(d(u)\) 表 ...