leetcode 46. Permutations(排列)

人形机器人专题:人形机器人关节,从Optimus选型看关节的发展 今天分享的是深度研究报告:《(报告出品方:中泰页。 阅读详情

在这里插入图片描述
返回数组nums中数字的所有可能的排列组合。

思路:

排列组合这种一般会想到DFS。

这个排列中每个数字只能用一次,
可用如下DFS流程

stack.push(num);
dfs(nums, num);
stack.pop();

退出条件:
当stack的size和nums数组一样时,说明已经完成了一个排列组合,保存结果退出当前dfs。
否则遍历数组,进入新一轮dfs.

每个数字只能用一次,所以用一个visited数组记录数字是否已经使用过。

class Solution {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        Stack<Integer> st = new Stack<>();
        boolean[] visited = new boolean[nums.length];
        dfs(nums, st, visited);
        return res;
    }

    void dfs(int[] nums, Stack<Integer> st, boolean[] visited){
        if(st.size() == nums.length) {
            List<Integer> list = new ArrayList<>(st);
            res.add(list);
            return;
        }
        //combination
        for(int i = 0; i < nums.length; i++) {
            if(visited[i]) continue;
            st.push(nums[i]);
            visited[i] = true;
            dfs(nums, st, visited);
            st.pop();
            visited[i] = false;
        }
    }
}

也可以不用stack. 直接在nums数组上模拟stack.
把需要装入stack的数字交换到前面,用一个指针模拟栈顶。
此方法更快。

class Solution {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        solve(nums, 0);
        return res;
    }

    void solve(int[] nums, int start){
        if(start == nums.length-1) {
            List<Integer> list = new ArrayList<>();
            for(int num : nums) list.add(num);
            res.add(list);
            return;
        }
        //combination
        for(int i = start; i < nums.length; i++) {
            swap(nums, start, i);
            solve(nums, start+1);
            swap(nums, start, i);
        }

    }

    void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
2024年电气工程师最新面试题及答案 1. **基本电气工程原理**:包括电流、电压、电阻、功率等基本概念。2. **欧姆定律**:V = IR,描述电压、电流和电阻之间的关系。42. **风力发电系统组成**:包括风力涡轮机、发电机、控制系统等。44. **智能电网技术**:包括自动化控制、通信技术、数据分析等。10. **减少EMI**:使用屏蔽、滤波、合理布线等方法。40. **光伏系统组成**:包括光伏板、逆变器、控制器等。48. **能量存储技术**:包括电池、超级电容器、飞轮等。 阅读详情

相关推荐

DINOv3实战指南:如何利用零样本视觉模型革新工业质检

本文详细介绍了如何利用DINOv3零样本视觉模型革新工业质检流程。通过无需标注数据的自监督学习技术,DINOv3能快速部署高精度检测系统,显著降低成本和提升效率。文章涵盖模型选型、数据准备、实时检测优化及典型行业解决方案,为工业质检提供革命性AI实践指南。

ansible6ops的博客 120

Leetcode 46. Permutations (全排列)

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

ysCover 373

从理论到实践:使用Sigrity SystemSI完成DDR4信号完整性仿真全流程解析

本文详细解析了使用Sigrity SystemSI完成DDR4信号完整性仿真的全流程,从核心挑战到实战操作。文章深入探讨了DDR4信号完整性问题,介绍了SystemSI工具链的演进与分工,并提供了从前期准备到高级技巧的详细指南,帮助工程师有效解决DDR4设计中的信号完整性问题。

weixin_30505225的博客 507

【LeetCode】 46. Permutations 全排列(Medium)(JAVA)

【LeetCode】 46. Permutations 全排列(Medium)(JAVA) 题目地址: https://leetcode.com/problems/permutations/ 题目描述: Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3...

qq_16927853的博客 248

Windows 下安装 Hermes Agent 完整教程(含踩坑记录)

不支持原生 Windows,必须通过 WSL2 (Windows Subsystem for Linux 2) 运行。WSL2 是 Windows 内置的 Linux 兼容层,可以在 Windows 上运行完整的 Linux 环境。

weixin_50191542的博客 4270

Ubuntu20.04下Intel RealSense D435i相机驱动配置全攻略(含ROS Noetic适配)

本文详细介绍了在Ubuntu20.04系统下配置Intel RealSense D435i相机驱动的完整流程,包括libRealsense SDK 2.0安装、ROS Noetic适配及常见问题解决方案。通过实战指南帮助开发者快速实现深度相机与ROS的集成,适用于机器人视觉和SLAM应用开发。

weixin_29281915的博客 511

Leetcode 46. Permutations 全排列

Leetcode 46. Permutations 全排列 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/permutations/ 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列。 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1...

CoderWangSon 256

02-Cesium-实现天地图标记

首先天地图申请token,然后Cesium-实现天地图标记详细说明

m0_62785037的博客 1171

LeetCode 46 全排列 permutations

LeetCode 46 全排列 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] ] 解题思路 求全排列问题,用递归 DFS 来求解。这里需要用到一个 visited 数组来标记某个数字是否访问过,然后在 DFS 递

qq_38801813的博客 205

✔B【AD623】【单运放】仪表放大器 AD623电压放大器模块可调 单电源 单端/差分 微小信号

【】仪表放大器 AD623电压放大器模块可调 单电源 单端/差分 微小信号

童话与梦的博客 8274

关于动态存储器dram中rank,bank,device之间的关系

DEVICE_WIDTH表示设备的带宽(单位bit),每个列(column)包含DEVICE_WIDTH 位(bit),每一行(row)包含NUM_COLS个column。   而每一个bank包含NUM_ROWS个行(row)。所以一个设备(device)的存储容量为PER_DEVICE_STORAGE =NUM_ROWS*NUM_COLS*DEVICE_WIDTH*NUM_BANKS (i

wyj7260的专栏 4729

LeetCode—46.全排列(Permutations)——分析及代码(C++)

LeetCode—46.全排列[Permutations]——分析及代码[C++]一、题目二、分析及代码1. 回溯法(1)思路(2)代码(3)结果三、其他 一、题目 给定一个没有重复数字的序列,返回其所有可能的全排列。 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] ...

江南土豆的博客 494

看GRE协议的数据封装

GRE网络数据封装流程

FanFF的博客 80

Leetcode 46. Permutations全排列

Given a collection ofdistinctintegers, 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] ] 题目链接:https://leetcod...

salmonwilliam的博客 458

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Leetcode: 46. Permutations 全排列

Permutations 全排列 给定一个没有重复数字的序列,返回其所有可能的全排列。 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 解法1:递归 vector<vector<int>> permute(vector<int>&amp...

Coding 466

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode第46题思悟——全排列(permutations)

LeetCode第46题思悟——全排列(permutations) 知识点预告 少做无用功; Collections.swap API的使用; 题目要求 给定一个没有重复数字的序列,返回其所有可能的全排列。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/permutations 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载...

博采众长,自成一派; 332

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[排列] leetcode 46 Permutations

problem:https://leetcode.com/problems/permutations/ 输出所有排列数。递归中,最快的方法是直接交换,实际上执行的是选择操作,选择了一个数据后,把它交换到前面;可以保证下一次选择不会包含着一数字,并且数字被选择的概率都是相等的。 class Solution { public: vector<vec...

ayzejx8547408的博客 134

Leetcode: 46. Permutations全排列

Permutations 全排列 给定一个没有重复数字的序列,返回其所有可能的全排列。 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 方法一: 用visited数组记录是否被访问。递归一次得到一个组合,之后将visited清零。 class Solution { pu...

Coding 239

leetcode-46. Permutations 全排列

  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] ] ...

别说话写代码的博客 302

Leetcode46. Permutations全排列

给定一个没有重复数字的序列,返回其所有可能的全排列。 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] class Solution { public: vector<vector<int> >re...

Ha12312的博客 149

【LeetCode】#46全排列(Permutations)

【LeetCode】#46全排列(Permutations) 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列。 示例 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] Description Given a collection of distinct integers, return a...

weixin_43858604的博客 186

LeetCode 46. 全排列(Permutations)

易错点正确做法说明没复制 path 导致结果串改用path[:]添加当前排列的副本没有回溯撤销操作pop()和remove()是回溯核心步骤忘记去重或状态标记使用used集合或布尔数组避免重复数字使用本题是回溯法中最基础的“全排列”问题,属于递归+状态记录的典型场景建议掌握以下三种形式:使用used集合记录访问状态使用used[i]数组(更高效)使用原地交换法(进阶优化,减少空间)

2501_91641025的博客 492

leetcode:46. Permutations 排列

46. Permutations问题描述Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1],

达哥是超人的博客 416

【Leetcode】46. 全排列(Permutations)

Leetcode - 46 Permutations (Medium) 题目描述: 给定无重复的整数序列,返回所有全排列的可能。 Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] public List<List<Integer>> per...

厉兵秣码 264

Leetcode # 46. Permutations 全排列 解题报告

1 解题思想这道题是全排列,做法很多,而且Leetcode上随后也会有更多的变种这道题的假设是:distinct ,不重复,所以是最简单的一个,问题解法有两种: 1、设立一个状态数组,标示是否选择了,然后递归的方式搜索所有可能的组合,这在我给的代码里是backtracking那个标记 2、从第一个数开始,不停的与他之后的数字进行交换,每当操作到了n次的一种组合后就加入列表,停止搜索,有dfs标记

MebiuW的专栏 1764

LeetCode 46. Permutations 全排列,树形状态回溯

46. Permutations Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3

老夫编代码就是一把梭 810
上一篇: ncnn的softmax
下一篇: leetcode 2616. 最小化数对的最大差值
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值