全排列问题(非递归实现和递归实现)

题目

  • 给定一个不含重复数字的数组 nums ,返回其所有可能的全排列

代码

1. 非递归实现,借助栈

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author: Li Qiang
 * @date: 2023/7/26
 * @description: 全排列问题的非递归实现
 */
public class Permutations {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1, 2, 3, 4};
        List<List<Integer>> permutations = solution.permute(nums);
        // 输出验证
        for (List<Integer> permutation : permutations) {
            System.out.println(permutation);
        }
    }
}
class Solution {
    public List<List<Integer>> permute(int[] nums) {
        int n = nums.length;
        // 定义结果列表
        List<List<Integer>> ans = new ArrayList<>();
        // 借助栈模拟递归过程
        Stack<List<Integer>> stack = new Stack<>();
        stack.push(new ArrayList<>());

        while (!stack.isEmpty()) {
            // currentPath代表当前的排列(不一定是全排列)
            // 其中的元素代表已将被当前排列采用的元素
            List<Integer> currentPath = stack.pop();
            if (currentPath.size() == n) {
                // 如果当前的排列是全排列,将其添加到结果列表中
                ans.add(currentPath);
            } else {
                // 如果当前的排列没有完成,通过添加未使用的元素来补充它
                for (int element : nums) {
                    if (!currentPath.contains(element)) {
                        // 如果当前排列的元素中不包含element,则采纳当前数组元素并将其放进新排列中
                        List<Integer> newPath = new ArrayList<>(currentPath);
                        newPath.add(element);
                        // 新的排列(不一定是全排列)暂时压入栈中
                        stack.push(newPath);
                    }
                }
            }
        }
        return ans;
    }
}

2. 递归实现

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Li Qiang
 * @date: 2023/7/26
 * @description: 全排列问题的递归实现
 */
class Solution {
    List<List<Integer>> ans;
    boolean[] visited;
    List<Integer> path;
    public List<List<Integer>> permute(int[] nums) {
        int n = nums.length;
        ans = new ArrayList<>();
        visited = new boolean[n];
        path = new ArrayList<>();
        
        loop(nums, 0, n);
        return ans;

    }
    public void loop(int[] nums, int len, int n) {
        if (len == n) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < n; i ++) {
            if (visited[i] == true) {
                continue;
            } else {
                visited[i] = true;
                path.add(nums[i]);
                loop(nums, len + 1, n);
                // 回溯
                path.remove(path.size() - 1);
                visited[i] = false;
            }
        }

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值