全排列

题目描述:给定一个不含重复数字的数组 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. Flink源码学习笔记(2) 基于Yarn的自动伸缩容实现

    1.背景介绍 随着实时计算技术在之家内部的逐步推广,Flink 任务数及计算量都在持续增长,集群规模的也在逐步增大,本着降本提效的理念,我们研发了 Flink 任务伸缩容功能: 提供自动伸缩容功能,可 ...

  2. 多源最短路-Floyd

    题目描述 时间限制:5.0s 内存限制:256.0MB 问题描述 给定\(n\)个结点两两之间的单向边的长度,求两两之间的最短路径. 输入格式 输入第一行包含一个整数\(n\),表示点数. 接下来\( ...

  3. Java安全之BCEL ClassLoader

    目录 Java安全之BCEL ClassLoader 写在前面 About BCEL 调试分析 食用姿势 Fuzz反序列化Gadget Fastjson BCEL Payload Thymeleaf ...

  4. 【第十三期】B站后端开发实习生一、二面经

    写在最前:非科班渣硕去年转码一年,不是什么大佬,纯小白(go语言开发). 一面(大概70min) 首先是自我介绍.(比较传统,就是描述下自己的技术栈) 线程和进程的关系. 线程之间如何进行通信. 死锁 ...

  5. python编写购物车-实时购买

    本次编写的是实时购买(输入商品直接进行购买),余额不足可以进行充值或结束购物 1 goods = [ 2 {"name": "电脑", "price& ...

  6. 普通web项目一直无法访问页面,直接报404

    错误:普通web项目一直无法访问页面,直接报404 原因:写了一个过滤乱码的类: 而在类上加了WebServlet注解,其实应该是WebFilter注解, 总结:基础不扎实,有些东西只会用,而不知道原 ...

  7. 让HTML和JSP页面不缓存从Web服务器上重新获取页面

    感谢原文作者:佚名 原文链接:https://www.jb51.net/web/100639.html 问题描述 用户退出后,如果点击浏览器上的后退按钮,Web应用将不能正确保护受保护的页面--在Se ...

  8. Java中静态变量与非静态变量的区别

    感谢大佬:https://www.cnblogs.com/liuhuijie/p/9175167.html ①java类的成员变量有俩种: 一种是被static关键字修饰的变量,叫类变量或者静态变量 ...

  9. pandas中常用的操作一

    pandas中常用的功能: 1.显示所有的列的信息,999表示显示最大的列为999 pd.options.display.max_columns=999 2.读取excel时设置使用到列的名称,和列的 ...

  10. java中LinkedList ArrayList 数组 HashSet 存储数据测试

    话不多少,直接上代码 import java.text.SimpleDateFormat;import java.util.*; public class testList { public stat ...