hgq's docs
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)

guoguoqiqi

漫不经心的向往
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)
  • 反转链表
  • 无重复字符的最长子串
  • LRU 缓存
  • 数组中的第K个最大元素
  • K个一组翻转链表
  • 三数之和
  • 手撕快速排序
  • 最大子数组和
  • 两数之和
  • 合并两个有序链表
  • 环形链表
  • 二叉树的层序遍历
  • 买卖股票的最佳时机
  • 二叉树的锯齿形层序遍历
  • 相交链表
  • 有效的括号
  • 二叉树的最近公共祖先
  • 合并两个有序数组
  • 搜索旋转排序数组
  • 最长回文子串
  • 岛屿的数量
  • 全排列
    • 字符串相加
    • 高频算法题
    guoguoqiqi
    2022-03-21

    全排列

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

    示例 1:

    输入:nums = [1,2,3]
    输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
    
    1
    2

    # 解法:回溯

    const permute = (nums) => {
        const res = [];
        const used = {};
    
        function dfs(path) {
            if (path.length == nums.length) { // 个数选够了
                res.push(path.slice()); // 拷贝一份path,加入解集res
                return;                 // 结束当前递归分支
            }
            for (const num of nums) { // for枚举出每个可选的选项
                // if (path.includes(num)) continue; // 别这么写!查找是O(n),增加时间复杂度
                if (used[num]) continue; // 使用过的,跳过
                path.push(num);         // 选择当前的数,加入path
                used[num] = true;       // 记录一下 使用了
                dfs(path);              // 基于选了当前的数,递归
                path.pop();             // 上一句的递归结束,回溯,将最后选的数pop出来
                used[num] = false;      // 撤销这个记录
            }
        }
    
        dfs([]); // 递归的入口,空path传进去
        return res;
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/permutations

    ← 岛屿的数量 字符串相加→

    最近更新
    01
    vuex数据持久化怎么做
    05-22
    02
    vue的动态路由怎么配置使用
    05-22
    03
    vue权限控制一般怎么做
    05-22
    更多文章>
    Theme by Vdoing | Copyright © 2022-2022 Guoquoqiqi | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式