From 0c04f30aba952c60a7ee381d85e260be28da5859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E6=96=87?= <30338551+YiwenXie@users.noreply.github.com> Date: Wed, 12 May 2021 22:25:52 +0900 Subject: [PATCH] =?UTF-8?q?Update=200046.=E5=85=A8=E6=8E=92=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新Java版本 --- problems/0046.全排列.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 5f7b1ac0..05d86785 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -147,7 +147,41 @@ public: Java: +```java +class Solution { + List> result = new ArrayList<>();// 存放符合条件结果的集合 + LinkedList path = new LinkedList<>();// 用来存放符合条件结果 + boolean[] used; + public List> permute(int[] nums) { + if (nums.length == 0){ + return result; + } + used = new boolean[nums.length]; + permuteHelper(nums); + return result; + } + private void permuteHelper(int[] nums){ + if (path.size() == nums.length){ + result.add(new ArrayList<>(path)); + return; + } + for (int i = 0; i < nums.length; i++){ + // if (path.contains(nums[i])){ + // continue; + // } + if (used[i]){ + continue; + } + used[i] = true; + path.add(nums[i]); + permuteHelper(nums); + path.removeLast(); + used[i] = false; + } + } +} +``` Python: