[LeetCode] 46. Int数组全排列 ☆☆☆(回溯)
描述
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
解析
和以前的字符串全排列一样。
官方题解:
回溯法 是一种通过探索所有可能的候选解来找出所有的解的算法。如果候选解被确认 不是 一个解的话(或者至少不是 最后一个 解),回溯算法会通过在上一步进行一些变化抛弃该解,即 回溯 并且再次尝试。
这里有一个回溯函数,使用第一个整数的索引作为参数 backtrack(first)。
如果第一个整数有索引 n,意味着当前排列已完成。
遍历索引 first 到索引 n - 1 的所有整数。Iterate over the integers from index first to index n - 1.
在排列中放置第 i 个整数, 即 swap(nums[first], nums[i]).
继续生成从第 i 个整数开始的所有排列: backtrack(first + 1).
现在回溯,即通过 swap(nums[first], nums[i]) 还原.
代码
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if (nums == null || nums.length <= 0) {
return list;
}
permuteHelp(nums, list, 0);
return list;
}
public void permuteHelp(int[] nums, List<List<Integer>> list, int startIndex) {
if (startIndex == nums.length) {
List<Integer> tempList = new ArrayList<>(nums.length);
for (int num : nums) {
tempList.add(num);
}
list.add(tempList);
}
for (int i = startIndex; i < nums.length; i++) {
swap(nums, i, startIndex);
permuteHelp(nums, list, startIndex + 1);
swap(nums, i, startIndex);
}
}
public void swap(int[] nums, int left, int right) {
if (left == right) {
return;
}
nums[left] = nums[left] ^ nums[right];
nums[right] = nums[left] ^ nums[right];
nums[left] = nums[left] ^ nums[right];
}
}
[LeetCode] 46. Int数组全排列 ☆☆☆(回溯)的更多相关文章
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- [LeetCode] 784. 字母大小写全排列 ☆☆☆(回溯、深度优先遍历)
https://leetcode-cn.com/problems/letter-case-permutation/solution/shen-du-you-xian-bian-li-hui-su-su ...
- [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###
描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode:寻找数组的中心索引【668】
LeetCode:寻找数组的中心索引[668] 题目描述 给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和 ...
- LeetCode初级算法--数组01:只出现一次的数字
LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
- LeetCode初级算法--数组02:旋转数组
LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- 每日一道 LeetCode (14):数组加一
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- 找出 int 数组的平衡点 & 二叉树 / 平衡二叉树 / 满二叉树 / 完全二叉树 / 二叉查找树
找出 int 数组的平衡点 左右两边和相等, 若存在返回平衡点的值(可能由多个); 若不存在返回 -1; ``java int [] arr = {2,3,4,2,4}; ```js const ar ...
随机推荐
- mysql命令、mysqldump命令找不到解决
1.解决bash: mysql: command not found 的方法 [root@DB-02 ~]# mysql -u root -bash: mysql: command not found ...
- jsplumb实现流程图
流程图使用工具汇总 jsPlumb,开源软件,推荐使用,参考学习链接: jsplumb学习笔记.基本概念.中文简易教程 jTopo myflow Go.js JointJS,属于商业软件 mxGrap ...
- 利用PHP应用程序中的远程文件包含(RFI)并绕过远程URL包含限制
来源:http://www.mannulinux.org/2019/05/exploiting-rfi-in-php-bypass-remote-url-inclusion-restriction.h ...
- iOS-UIScreen,UIFont,UIColor,UIView,UIButton
6.1 UIScreen // 屏幕的宽度 CGFloat screenW = [UIScreen mainScreen].bounds.size.width; 6.2 UIFont + (UIFon ...
- docker:如何查看容器的挂载目录
docker inspect container_name | grep Mounts -A 20 docker inspect container_id | grep Mounts -A 20 [r ...
- mysql的AB及读写和集群
Mysql的AB及读写 第1章 Mysql的AB配置 1.1 master配置 1.2 slave配置 第2章 读写分离 2.1 安装mycat 2.2 启动mycat 2.3 登录mycat相关问 ...
- ROS下gazebo打开出现问题
通常情况下,在装完ros后,打开gazebo, 会出现无法连接服务器获取模型的情况.这样,我们打开gazebo, 终端会提示timeout,gazebo界面没有网格. 这是因为服务器网站地址好像已经换 ...
- Java面试笔记整理4
一.Java内存溢出的产生原因和解决办法? java.lang.OutOfMemoryError这个错误我相信大部分开发人员都有遇到过,产生该错误的原因大都出于以下原因:JVM内存过小.程序不严密,产 ...
- Java 最常见 200+ 面试题 + 全解析
本文分为十九个模块,分别是: Java 基础.容器.多线程.反射.对象拷贝.Java Web .异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Spring Clou ...
- consul 初体验
consul server: 192.168.48.134: #!/bin/bash cd /data/server/consuls nohup /data/server/consuls/consul ...